开发者

Smarty html_options

开发者 https://www.devze.com 2022-12-24 18:06 出处:网络
For smarty\'s html_options function, is there a way to avoid having to do this (other than not using smarty that is)?

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, strayi开发者_JAVA技巧ng 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:""}


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}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号