Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Can you include templates in Template Toolkit without specifying the file extension?

by shadowsong (Monk)
on May 16, 2017 at 17:23 UTC ( #1190408=note: print w/replies, xml ) Need Help??


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.

Generating Output

tpage

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.tt

ttree

This 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.

Step 1 - generate base .ttreerc default file

ttree

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.

Step 2 - create site directory structure

~/ |__dist |__templates |__cfg |__trixietang.co.uk |__src |__lib

Step 3 - edit ttree config files

.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

Step 4 - put include files in ~/templates/trixietang.co.uk/lib

~/ |__dist |__templates |__cfg | |__trixietang.co.uk.conf |__trixietang.co.uk |__src |__lib |__header.tt |__footer.tt

Step 5 - create source template files in ~/templates/trixietang.co.uk/src

~/ |__dist |__templates |__cfg | |__trixietang.co.uk.conf |__trixietang.co.uk |__src | |__example.tt | |__index.html.tt |__lib |__header.tt |__footer.tt

Step 6 - run ttree command from ~/templates/cfg

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

  • Comment on Re: Can you include templates in Template Toolkit without specifying the file extension?
  • Select or Download Code

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 17:59 UTC
    You guys really should make switch to Mojolicious! xD
      Such gratuitious remarks are just a waste of photons and electrons.

      Of course you can redeem ypourself by explaining why mojolicious is so much better.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-12-19 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (83 votes). Check out past polls.

    Notices?