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

How do I select an option according to a specified value in HTML::Template::Pro?

by PerlOnTheWay (Monk)
on Aug 11, 2011 at 04:43 UTC ( [id://919793]=perlquestion: print w/replies, xml ) Need Help??

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

<select name="province_id" id="province"> <option value="-1">--</option> <TMPL_LOOP NAME="provinceloop"> <option value="<TMPL_VAR NAME="id">"> <TMPL_VAR NAME="name"></option> </TMPL_LOOP> </select>
The above is my code to show a select element populated with various option. But how do I add selected=true to those options when <TMPL_VAR NAME="id"> eq <TMPL_VAR NAME="current_key">?

Replies are listed 'Best First'.
Re: How do I select an option according to a specified value in HTML::Template::Pro?
by moritz (Cardinal) on Aug 11, 2011 at 10:17 UTC

    HTML::Template and its derivative HTML::Template::Pro don't support the kind of conditionals you need for that, just simple bools. So what you need to write is

    <select name="province_id" id="province"> <option value="-1">--</option> <TMPL_LOOP NAME="provinceloop"> <option value="<TMPL_VAR NAME="id">" <TMPL_IF is_selected>selected="true"</TMPL_IF>> + <TMPL_VAR NAME="name"></option> </TMPL_LOOP> </select>

    And in the the perl code populate is_selected with a true value for the option where id eq current_key.

Re: How do I select an option according to a specified value in HTML::Template::Pro?
by tinita (Parson) on Aug 11, 2011 at 11:22 UTC
    Just for the records, if you have the possibility to use HTML::Template::Compiled and the plugin HTML_Tags:
    Template:
    <tmpl_html_option province>

    Perl:
    $htc->param( province => [ 'opt_2', # selected ['opt_1', 'option 1'], ['opt_2', 'option 2'], ], );
    Output:
    <option value="opt_1">option 1</option> <option value="opt_2" selected="selected">option 2</option>
Re: How do I select an option according to a specified value in HTML::Template::Pro?
by wfsp (Abbot) on Aug 11, 2011 at 09:52 UTC
    I think I might check if id was the current_key in my code and set a param to true or false as required.
    <TMPL_LOOP NAME="provinceloop"> <option value = "<TMPL_VAR NAME = "id">" selected = "<TMPL_VAR NAME = "current_key"> > <TMPL_VAR NAME="name"> </option> </TMPL_LOOP>
Re: How do I select an option according to a specified value in HTML::Template::Pro?
by fisher (Priest) on Aug 11, 2011 at 10:02 UTC
    I think I have to disappoint you -- the above is not *perl* code, and even have nothing in common with programming; it is hypertext *Markup* Language.

    How do I post a question effectively?

      It may not be Perl code, but it is a Perl question. The sample is HTML with HTML::Template mark up added. If you deal with HTML generation at all then you would do very well to take a look at the module.

      Oh, and while we are disabusing notions: the language is Perl. The interpreter is perl. This is a common shibboleth in Perl speaking circles.

      True laziness is hard work
        Thank you - I've learned new (for me) word.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found