Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: HTML - sharing design and logic

by muba (Priest)
on Jul 01, 2004 at 13:00 UTC ( #371066=note: print w/ replies, xml ) Need Help??


in reply to Re: HTML - sharing design and logic
in thread HTML - separating design and logic

Well, I'd say you could do this by placing the select tag within the template, then use a loop to place all the options and use the template's IF tag to insert the 'selected' keyword.


Comment on Re^2: HTML - sharing design and logic
Re^3: HTML - sharing design and logic
by hardburn (Abbot) on Jul 01, 2004 at 13:13 UTC

    Yes, you can do it that way, and many people do. It makes the templates really, really ugly. If there is a clean solution to this, I haven't seen it yet. Ironically, some of the cleanest ones I've seen involve creating the HTML on the Perl side (though usually with a backend module doing the actual HTML generation).

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      It makes the templates really, really ugly.

      Depends on how you do it. Myself, I use Template Toolkit and right off the bat I created a widgets/ directory in my base template directory. This widgets directory contains a different template for each of the form fields (text inputs, textareas, radio buttons, select fields, etc). My select field template is exactly what is below. I'm not even sure now if the SET option.selected = '' line is needed. I think I had to add it because of some weird complication (perhaps the option.selected value wasn't being reset in each iteration of the loop?).

      [% USE HTML %] [% DEFAULT size = 1 %] <select name="[% HTML.escape(name) %]" size="[% size %]" class="frmSel +ect"> [% FOREACH option = options %] [% SET option.selected = ''; SET option.selected = ' selected="selected"' IF option.value = += selected; DEFAULT option.label = option.value; %] <option value="[% HTML.escape(option.value) %]"[% option.selected +%]>[% HTML.escape(option.label) %]</option> [% END %] </select>

      Now within any html template that wants to include a select field, all it takes it this simple include:

      [% INCLUDE widgets/select.tmpl name="fav_flavour" selected="$fav_flavour" options=[ { value => 'Chocolate', label => 'brown stuff' }, { value => 'Vanilla', label => 'plain stuff' }, { value => 'Strawberry', label => 'red stuff' } ] %]

        Myself, I use Template Toolkit . . .

        Increasing the complexity of the template isn't a real solution, because at that point you're no longer doing seperation of data and code, which is the whole point.

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      Perhaps our standards of beauty differ, but:
      <select> <tmpl_loop name='options'> <option value='<tmpl_var val>' <tmpl_if selected > selected </ +tmpl_if> </option> </tmpl_loop> </select>
      Is that really so ugly?

        I was thinking of the situation where the HTML designer wanted to include the list statically:

        <select> <option value="1" <TMPL_IF option1>selected</TMPL_IF>>1</option> <option value="2" <TMPL_IF option2>selected</TMPL_IF>>2</option> <option value="3" <TMPL_IF option3>selected</TMPL_IF>>3</option> . . . </select>

        Belch!

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://371066]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-21 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls