简体   繁体   中英

Smarty html_options

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?

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

I know it's like 12y later but... While migrating an app to php 8.1 I just hit this same issue:) So the real solution that worked was

{html_options name=option_1 options=$options selected=$default.option_1|default:""}

事实证明,如果不编写单独的插件,我想要的东西是不可能的……也许我会这样做,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM