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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Another feature of TT that works well with blocks is the macro stuff. It allows you to write "functions" to do various things. i.e.

this takes a scalar and a list or scalar. If the scalar matches the other scalar or matches a value in the list the macro resolves to "selected" otherwise it does nothing. (used to find selected values in a <select> list.

[% # sets selected if both values match # tries to see if either side is an array to try and find a match # out of an array of possibly selected values. This will work for m +ultiple # select lists. %] [%- MACRO is_selected(val1, val2) BLOCK -%] [% IF val1 == val2 %] selected [%- selected = 1 %] [% END %] [% IF !selected %] [%- FOREACH value = val1 -%] [%- IF value == val2 -%] selected [%- selected = 1 -%] [%- LAST -%] [%- END -%] [%- END -%] [% END %] [%- IF !selected -%] [%- FOREACH value = val2 -%] [%- IF value == val1 -%] selected [%- LAST -%] [%- END -%] [%- END -%] [%- END -%] [%- END -%]
This lets you do things like. . . .
[% # selected items is a list of what should be selected # from this list %] <select name=foo> [% FOREACH select_item = option_list.keys %] <option value="[% option_list.$select_item.value %]" [% is_select +ed(option_list.$select_item.value, selected_items %]> [% option_list.$select_item.display_name %] </option> [% END %] </select>
I have not seen anything in HTML::Template that will let me come close to being able to do this. Most of the work that the template is doing would have to be shifted into the perl code. with html::Template (afaik, I stopped using after I got approval to use TT). In the above example option_list is a hash (that for sake of argument I got from a library call that I don't control). In H::T I'd have to re-write that structure into an array, I'd also have to resolve the selected attribute inside the perl code. The only place where those things are needed are for the display, so why should they be in the perl code.

Granted, there is logic in the templates now, but it is solely DISPLAY logic. It is not processing logic. The split I try to get is to not have ANY display logic in the perl code and to not have ANY processing logic in the template. Stuff like properly formatting a date gets put into the template. Stuff like cooking data before it is put into the database lives in the perl code. There seems to be a fairly large camp that does not want to put any logic inside the templates. They are right too. :)

Sorry if this is a bit long winded but I really enjoy working with this stuff.


In reply to Re^3: Code and html separation - always or mostly doable? by amw1
in thread Code and html separation - always or mostly doable? by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found