Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
[marioroy]: choroba++, Discipulus++. It depends on the type of module. Data-type "only" modules are likely multi-process safe, re: Hash::Ordered, Tie::IxHash.
[marioroy]: ... when shared via MCE::Share-> share(...)
[marioroy]: Net type modules are likely not multi-process safe unless stated in the documentation.
[marioroy]: The Prima author fixed his module to be both thread and multi-process safe. Thanks Dmitry.
[marioroy]: Of all the GUI-type modules, Prima was the worst regarding thread/multi- process safety. Now, it's the best for safety. ;-)
[marioroy]: Tk, Gtk2, Gtk3 requires extra care.
[marioroy]: I was unable to run parallel with Win32::Console. Fortunately, running parallel behind Win32::API is possible.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2017-09-22 10:16 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (260 votes). Check out past polls.