in reply to Can you include templates in Template Toolkit without specifying the file extension?
Hi TrixieTang
Here are a couple of things that might help – there are two ways to “attempt to use it” both of which ought to be installed as part of Template::Toolkit.
You need to specify the extension of the include files (and location of said include files if they're not in the current working directory). So your example.tt file will look like this:
[% INCLUDE header.tt title = 'This is an HTML example'; pages = [ { url = 'http://foo.org' title = 'The Foo Organisation' } { url = 'http://bar.org' title = 'The Bar Organisation' } ] %] <h1>Some Interesting Links</h1> <ul> [% FOREACH page IN pages %] <li><a href="[% page.url %]">[% page.title %]</a> [% END %] </ul> [% INCLUDE footer.tt %]
…and the command to generate your output could look something like:
tpage --relative --include_path='relative path/to/header.tt and footer.tt' example.ttThis requires a bit more setup and can be fiddly at first but is worth it…
Let's assume you're working on a site called trixietang.co.uk and Template::Toolkit has already been installed.
This will prompt you to create a sample .ttreerc file - optional but useful step to help you understand what the various directives are in Step 3 below.
~/ |__dist |__templates |__cfg |__trixietang.co.uk |__src |__lib
.ttreerc – tweak entries in ~/.ttreerc (or $HOME\.ttreerc if you're on Win32)
# options to rewrite files suffixes (htm => html, tt2 => html) # # suffix htm=html # suffix tt2=html suffix html.tt=html
trixietang.co.uk.conf – create a new file: ~/templates/cfg/trixietang.co.uk.conf and add the following contents:
# options to define dependencies between templates # # depend *=header,footer,menu # depend index.html=mainpage,sidebar # depend menu=menuitem,menubar depend *=header.tt,footer.tt # copy everything except .tt files; those ought to be processed copy = \.(php|pl|cgi|ico|jpg|pdf|scssc|scss|css|map|eot|svg|otf|ttf|wo +ff|woff2|js|ics|less|eps|pdn|tif|xml)$ # directory containing source page templates src = ../trixietang.co.uk/src # directory where output files should be written [~/dist] dest = ../../dist/trixietang.co.uk # additional directories of library templates lib = ../trixietang.co.uk/lib
~/ |__dist |__templates |__cfg | |__trixietang.co.uk.conf |__trixietang.co.uk |__src |__lib |__header.tt |__footer.tt
~/ |__dist |__templates |__cfg | |__trixietang.co.uk.conf |__trixietang.co.uk |__src | |__example.tt | |__index.html.tt |__lib |__header.tt |__footer.tt
cd ~/templates/cfg ttree -p -f trixietang.co.uk.conf
If all goes well; you should then see your output files generated under ~/dist/trixietang.co.uk stripped of their tt suffix…
~/ |__dist | |__trixietang.co.uk | |_example | |_index.html |__templates |__cfg | |__trixietang.co.uk.conf |__trixietang.co.uk |__src | |__example.tt | |__index.html.tt |__lib |__header.tt |__footer.tt
Feel free to play around with your ttree config files from Step 3 – altering depends and dest as required.
Good Luck!
Shadowsong
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can you include templates in Template Toolkit without specifying the file extension?
by Anonymous Monk on May 16, 2017 at 21:59 UTC | |
by CountZero (Bishop) on May 17, 2017 at 07:12 UTC |