Re: Collecting snippets

Andy Wardley <[email protected]> Wed, 13 Jun 2012 15:58:12 +0100
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
On 13/06/2012 15:22, Dave Howorth wrote:
> I'm sure there must be a way to do this, and I'm guessing it probably
> involves WRAPPER, but my brain is fried. Can anybody give me a hint?

Hi Dave,

You could have a PRE_PROCESS template define a list for keywords and a 
MACRO to add a keyword to it:

   [% keywords = [ ];

      MACRO keyword(k)
        CALL keywords.push(k);
   -%]

And then define a WRAPPER template that looks something like this:

   <html>
     <head>
       <meta name="keywords" content="[% keywords.join(',') %]">
     </head>
     <body>
       [% content %]
     </body>
   </html>

And then in your page templates, call the keyword() macro to register 
each keyword:

   [% keyword('wibble') %]

The inner page templates get processed (into 'content') before the outer 
wrapper is invoked, so once you're in the wrapper template, you can 
safely print the keywords in the header and the generated content below 
it in the body.

If it's not convenient to call that keyword() macro explicitly then you 
might prefer to define a subroutine, filter, etc., to filter the page 
content and extract the keywords.  In which case you wouldn't need the 
PRE_PROCESS template and could have a WRAPPER template something like this:

   <html>
     <head>
       <meta name="keywords"
             content="[% your_keyword_extracting_function(content) %]">
     </head>
     <body>
       [% content %]
     </body>
   </html>

Cheers
A