views:

712

answers:

2

For smarty's html_options function, is there a way to avoid having to do this (other than not using smarty that is)?

{if $smarty.post}
    {html_options name=option_1 options=$options selected=$smarty.post.option_1}
{else}
    {html_options name=option_1 options=$options}
{/if}

I realize that it won't show up in the template, but it seems like a bad practice to leave something that is not defined in the template (it also fills up my error logs with noise about undefined indexes).

[edit]

What I am looking for is a way to do it like this without having the undefined index errors show up, as well as reducing the smarty noise in the template files.

{html_options name=option_1 options=$options selected=$smarty.post.option_1}

I guess it would more likely be a modified html_options plugin?

[edit]

As per @mmcgrail's idea:

{if isset($smarty.post.option_1)}
    {assign var=selected value=$smarty.post.option_1}
{else}
    {assign var=selected value=$default.option_1}
{/if}

{html_options name=option_1 options=$options selected=$selected}

I find this even worse because it is creating new variables in the template, straying from the supposed goal of smarty.

I guess this works:

or:

<?php
    //[... snip ...]
    $option_1 = isset($_POST['option_1'])? $_POST['option_1'] : $default['option_1'];
    $template->assign('option_1', $option_1);
    $template->display('my_template.tpl');

And in the template:

{html_options name=option_1 options=$options selected=$option_1}

But then what is the point of smarty keeping track of all of the post/get/request/cookie/server/constants if you can't use them in the template without doubling the amount of code you have to write?

A: 

try this

 {if isset($smarty.post)}
     {html_options name=option_1 optins=$options selected=$smarty.post.option_1}
 {/if}

i think that answer your question

mcgrailm
just noticed that your doint that in the php my solution is goes in template
mcgrailm
Nah, this is in the template... Your solution is the same as mine and results in twice as much code needing to be written... which is what I _thought_ was the point of smarty... apparently not
SeanJA
Also, you don't need the isset unless you are looking at a specific item in the post array.
SeanJA
A: 

Turns out that without writing a separate plugin what I want is not possible... maybe I will do that, something like:

{html_options name=option_1 options=$options selected=$default.option_1 post=option_1}
SeanJA