Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

template toolkit question -- metadata

by madison.sacd (Novice)
on May 11, 2007 at 06:29 UTC ( #614822=perlquestion: print w/replies, xml ) Need Help??
madison.sacd has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am trying to show different title in template.

In my controller, I assign a type variable to the template.
$c->stash->{type} = 'B';
In the template, according to the type, the metadata "title" will be assigned different value, "Book" or "CD". My problem is that I got correct debug output for each condition. However, "CD" will always be assigned to the metadata "title". In other words, when I pass type 'B' to template, the debug output shows "!!!!!!!!!!", which is correct, but the title displays "CD". Anything wrong with my code?
[% IF type == 'B'; META title = 'Book'; debug("!!!!!!!!!!"); debug(type); ELSE; debug("@@@@@@@@@@"); debug(type); META title = 'CD'; END; -%]

Replies are listed 'Best First'.
Re: template toolkit question -- metadata
by Tomte (Priest) on May 11, 2007 at 08:27 UTC

    The TT2 documentation says:

    The META directive allows simple metadata items to be defined within a template. These are evaluated when the template is parsed
    That means that the last value assigned to a META item will 'win', so to speak, as the conditional can't be and isn't evaluated during parse-time - change the order of the conditional, and the title should always display "BOOK".

    Edit: I experimented a bit with the following template

    [% SET type = "D" %] [% IF type == 'B'; META title = 'Book'; ELSIF type == 'D'; META title = 'DVD'; ELSE; META title = 'CD'; END; -%] - [% template.title %] / [% type %] / [% %] / [% templa +te.modtime %] - [% META title = 'LAST' %]
    and ttree to test my hypothesis, and I seem to interpret the documentation correctly:
    - LAST / D / test.tt2 / 1178881986 -


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: template toolkit question -- metadata
by Anonymous Monk on May 11, 2007 at 09:53 UTC
    type == 'B' you are using a numeric comparison as opposed to a string comparison. Try 'eq' instead of '=='. This would generally mean that it would default to false. If you change the else to an elseif type == 'C' and add another else then im pretty sure it will default to the else and not default to CD all the time. that way u can test to see if any of your comparisons are working or not ^_^

      OP knows the comparisons are working because of the debug-statements; the code is not Perl, but TemplateToolkit2 - and there was no eq in TT2 the last time I took a look (take a look for yourself)


      An intellectual is someone whose mind watches itself.
      -- Albert Camus

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://614822]
Approved by bingos
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-03-24 17:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (305 votes). Check out past polls.