Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by TrixieTang (Sexton)
on May 16, 2017 at 18:41 UTC ( [id://1190399]=perlquestion: print w/replies, xml ) Need Help??

TrixieTang has asked for the wisdom of the Perl Monks concerning the following question:

In the documentation for Template Toolkit it shows examples such as [% INCLUDE header %] being used - but I can't for the life of me find anything in the documentation about how to include files that way without having to specify the extension.

For example, I can get [% INCLUDE header.html %] or [% INCLUDE header.tt2 %] to work, but it gives a template not found error if I attempt to use something like [% INCLUDE header %].

Is there a way to set the extension that Template Toolkit searches for when none is specified in the include block?

  • Comment on Can you include templates in Template Toolkit without specifying the file extension?

Replies are listed 'Best First'.
Re: Can you include templates in Template Toolkit without specifying the file extension?
by beech (Parson) on May 16, 2017 at 19:23 UTC

      This is exactly what I was looking for. Thanks.

      I currently have it working, but the '.html' bit is hard coded into the provider that I made (your code worked fine without any tweaks BTW), is there some a way to pass one of my configuration variables to the provider so the template extension isn't hard coded into the provider and can be changed from a config file?

        See the synopsis section of Template::Provider::ArchiveTar on how you can pass parameters to your own template provider.

        This will mean that you will need to instantiate your own Template Toolkit object.

Re: Can you include templates in Template Toolkit without specifying the file extension?
by choroba (Cardinal) on May 16, 2017 at 19:13 UTC
    The syntax [% INCLUDE header %] is usually used for block inclusion, i.e. somewhere in the same file, or in the file from which the template has been called, the block of the name header should be defined. See INCLUDE.

    If no such block existed, file named header (i.e. with no extension) would be included.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Can you include templates in Template Toolkit without specifying the file extension?
by shadowsong (Pilgrim) on May 16, 2017 at 21:23 UTC

    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…

    Good Luck!
    Shadowsong

      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
Re: Can you include templates in Template Toolkit without specifying the file extension?
by karthiknix (Sexton) on May 18, 2017 at 12:08 UTC

    In Template Toolkit you can include a block of code inside the ttml file or altogether an another ttml file. For example you have ttml file called "test" under lib folder. Below is how it is coded

     [% INCLUDE lib/test %]

    or you have a block of code in the same file below is how coded.

    [% INCLUDE perlmonks %] [% BLOCK perlmonks %] <div>test</div> [% END %]

    I am not sure you can call file with any other extension apart from ttml which do not have any extension.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1190399]
Approved by beech
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found