syrusakbary / pyjade

Jade template system for Django, Jinja2 and Mako
MIT License
699 stars 122 forks source link

PyJade include differs from Jade include #135

Open command-tab opened 10 years ago

command-tab commented 10 years ago

I noticed when using include with pyjade 2.0.3 with Flask (Jinja2), the path that's specified needs to be relative to the Jinja templates directory. When using the same templates under Jade, those include paths are instead relative to the directory of the template performing the include.

Here's an example where I have a task list template that should include task detail as well as user list templates.

templates/tasks/list.jade:

//- This line works only under Jade
//- To make it work in PyJade, it needs to be: include tasks/detail
include detail

//- This line works only under PyJade
//- To make it work in Jade, it needs to be: include ../users/list
include users/detail

templates/tasks/detail.jade:

p Task detail

templates/users/list.jade:

p User list

This is frustrating because I would like to use the jade npm module to validate the syntax of my templates, but because the include behavior differs between the two implementations, jade chokes on things that are valid to PyJade and vice-versa.

blx commented 9 years ago

This is annoying, yes, but it's mostly a problem with Jinja, not PyJade.

PyJade for jinja is implemented as a jinja extension that preprocesses source templates on demand and does no actual file loading -- pyjade just gets passed the template source that jinja loaded, translates it into jinja syntax (so include x becomes {% include "x.jade" %}), and returns it to jinja.

It looks like you can modify your jinja environment to do what you want, though:

The issue here is that Jade supports both relative and absolute (relative to jade's basedir) paths for include and extend, where the absolute paths are prefixed with /, but Jinja's commonly used template loaders treat all paths as absolute from the root template directory.

The alternative, I guess, would be for pyjade to do some fancy include-rewriting when using jinja mode, for example. Maybe it could be enabled with an option. But that might not play nicely with various jinja Loaders.