Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Template::Toolkit - uc() and lc(), other simple formatting mutators - see TT Filters!

by jaa (Friar)
on Apr 23, 2008 at 10:47 UTC ( [id://682357]=perlquestion: print w/replies, xml ) Need Help??

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

Folks,

Blush for asking a newbie Template::Toolkit question! I am more looking for general pointers in the right direction, (I know there is always a pain barrier when getting my head around a new tool...)

As an e.g. in my playing around I want to:

[% uc(myvar) %]
But the only way I have found to do this is:
[% use String %] [% String.new(myvar).upper() %]

This is a trivial example - I can imagine that I will end up implement my own class with ALL my common view mutator - upper() lower() proper() escape() commify() etc

Yes, I know about HTML.escape() and I am sure that there are date classes with formatting mutators...

Is this what everyone else does? Is there a 'better way'?

Actually I am hoping for a 'Doh!' when you point out that these are all built-ins in TT and that I have not found the right docs!

I am reluctant to do this formatting in the Model components as I see it as part of rendering an HTML view (eg escape(). I also think it kind-of breaks the 'keep it easy for template tweakers' principle.

Many thanks for your pearls!

Jeff

Replies are listed 'Best First'.
Re: Toolkit::Template - uc() and lc(), other simple formatting mutexes
by tachyon-II (Chaplain) on Apr 23, 2008 at 11:16 UTC

    You have not found the right docs assuming you mean Template::Toolkit not Toolkit::Template which AFAIK does not exist. There are a swag of standard FILTERs you can use.

    [% "hello world!" FILTER ucfirst %]

      Doh! and fab! many thanks, I knew I was missing something!

      And this shows me how to easily create new TT filters in the Catalyst framework... (eg commify ucwords etc!) http://readlist.com/lists/lists.rawmode.org/catalyst/2/11553.html

      package myApp::View::TT; use strict; use base 'Catalyst::View::TT'; __PACKAGE__->config({ CATALYST_VAR => 'catalyst', ... FILTERS => { yesno => sub { $_[0] ? 'yes' : 'no'; }, onoff => sub { $_[0] ? 'on' : 'off'; }, }, });
      Many many thank Monks!
Re: Toolkit::Template - uc() and lc(), other simple formatting mutexes
by haoess (Curate) on Apr 23, 2008 at 11:13 UTC
    [% use String %] [% String.new(myvar).upper() %]

    Nice, but why not use a Filter:

    [% myvar | upper %]

    -- Frank

Re: Template::Toolkit - uc() and lc(), other simple formatting mutators - see TT Filters!
by perrin (Chancellor) on Apr 23, 2008 at 13:52 UTC
    If you don't find a filter that does what you want, just make your own sub ref and pass it to the template.
    my $ucfirst = sub { ucfirst($_[0]) }; $template->process($template_name, { %vars, ucfirst => \&ucfirst });
    And then in your template...
    [% ucfirst(foo) %]
mutex?
by ForgotPasswordAgain (Priest) on Apr 23, 2008 at 11:00 UTC

    I don't know much about TT, so could only give smart-alecky advice like "use HTML::Mason instead" or something.

    But I'm pretty sure you're using the word "mutex" incorrectly, unless there is some subtlety about locking templates that I'm missing.

      opps - sorry! mutex -> mutator

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found