Edgewall Software

Opened 15 years ago

Last modified 14 years ago

#367 new enhancement

Permit xi:include references to be search_path relative

Reported by: db3l.net@… Owned by: cmlenz
Priority: minor Milestone:
Component: Template processing Version: 0.5.1
Keywords: Cc:

Description

I have cases where I have a common set of include templates for a site, but the parent templates are in their own sub-directories for simpler management. For example:

  <search_path_root>/
      include/layout.xhtml
      actions/
          move/pick_class.xhtml

Now, inside pick_class.xhtml I have to use a relative include path for xi:include of "../../include/layout.xhtml" which makes that file fragile in the presence of re-arranging the template directory structure.

One workaround I've used is to create a context variable containing the target root and pass that to templates, so the include href becomes something like "${tmpl_root}/include/layout.xhtml", but that seems clumsy at best and more problematic with multiple search roots.

What I'd like to be able to do is specify the xi:include href in a way such that it was relative to the same search root as the templates. I've done this locally by patching base.py such that an absolute xi:include path ("/include/layout.xhtml") becomes a relative load (strip the leading "/") but without a relative_to argument so relative to the search path. I'm not absolutely sure that's the best place for the fix (as opposed to a new parameter or behavior in the TemplateLoader.load() perhaps).

But it seems that some mechanism to permit nested includes to obey the same search path in the loader as their parent template would help improve the robustness of nested template structures, in some directory layouts, such as mine.

-- David

Attachments (1)

base.py.patch (2.8 KB) - added by db3l.net@… 14 years ago.
Absolute template reference patch

Download all attachments as: .zip

Change History (4)

comment:1 Changed 14 years ago by wickedgrey@…

AFAICT, this also causes problems for package-based templates:

_loader_dict[packagename] = loader.package(packagename, 'templates')

templateLoader = TemplateLoader?(loader.prefixed(_loader_dict), max_cache_size=100*len(_loader_dict))

From the python API, I can call templateLoader.load('pkg/example.xhtml') but I can't <xi:include href="otherpkg/example2.xhtml"/> from inside the template, nor can I use href="/otherpkg/example2.xhtml"; I have to use href="../otherpkg/example2.xhtml" which is, as stated above, fragile (not to mention awkward).

Being able to include a leading slash in the path for both templateLoader.load and xi:include would be very nice.

comment:2 Changed 14 years ago by hodgestar

@David: Would you mind uploading the patch you created so I can have a look at it?

comment:3 Changed 14 years ago by db3l.net@…

Sure - attaching now (patch still relative to 0.5.1). The fact that I needed to check for absolute references in two distinct places is part of why I wondered if pushing the feature upstream a bit into TemplateLoader would make more sense, but as is it's a simple enough change.

-- David

Changed 14 years ago by db3l.net@…

Absolute template reference patch

Note: See TracTickets for help on using tickets.