porton
porton

Reputation: 5803

Template Toolkit: Extracting a part of a template

I was assigned the task to rewrite our home-made templates with Perl Template Toolkit.

Our templates have the possibility to extract a fragment of the template (and then make HTML from it) instead of using the entire template.

How to do it with Template Toolkit?

The best solution I came with is the following:

Let we have a template x.html with fragment A.

The fragment should be extracted into new file. (I am to name it like x/A.html for consistency.) In the original template x.html it should be replaced with [% INCLUDE 'x/A.html' %]. So I could be able to use either the entire template x.html or its fragment x/A.html. Probably we may have several levels of inclusion like x/A/B.html.

Are there other ways to do it? (I don't like to idea to split it in subdirectories as described above, but haven't come up with a better solution.)

Upvotes: 1

Views: 93

Answers (1)

Sam Graham
Sam Graham

Reputation: 1631

Are you asking whether there's a better way to extract the fragment from the parent template?

(Answer is: no, that's probably the best way.)

Or are you asking is there a better way to organize the extracted fragements?

(Answer is: no real best answer, everywhere will have their own house style - you aren't going to go too far wrong by picking any convention.)

Most common conventions for naming I've seen are subdirectories x/A.html and prefixes x_A.html. Whether you use the name of the parent template for x or you choose to group by functionality as simbabque suggested is another matter of taste: grouping by functionality scales better on larger and more complicated layouts where you have a great deal of reuse of components, but grouping by use case is conceptually simpler in smaller use cases with little or no component reuse.

Upvotes: 2

Related Questions