0

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?

SeanJA
  • 10,234
  • 5
  • 32
  • 42
  • what are the errors you gut when you put the one line int ? – mcgrailm Mar 25 '10 at 12:14
  • When the post variable is empty I get "NOTICE: Undefined index: option_1" I realise it is not a horrible horrible error, but it seems sloppy to leave it in there. – SeanJA Mar 25 '10 at 13:09
  • Another problem is that the notice shows up hidden in the select box if you inspect the html. – SeanJA Mar 25 '10 at 13:21
  • that's why you need to use isset somewhere because your array value is not set your could in your php do the isset and define a variable that you pass to smarty as $selected and then you won't get the error – mcgrailm Mar 25 '10 at 14:18
  • 1
    I do wish that I didn't have to use smarty.... – SeanJA Mar 25 '10 at 23:07
  • e point of smarty is not to write less code but to seperate as much of the code as possible from the html so that your not echoing all the time and so that your can display various pieces, you know what I mean – mcgrailm Mar 26 '10 at 16:51
  • @mmcgrail "So you are not echoing all the time" does seem like it is trying to make it so I can write less code. – SeanJA Mar 29 '10 at 16:46

3 Answers3

1

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
  • 17,469
  • 22
  • 83
  • 129
  • just noticed that your doint that in the php my solution is goes in template – mcgrailm Mar 24 '10 at 20:26
  • 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 Mar 24 '10 at 23:00
  • Also, you don't need the isset unless you are looking at a specific item in the post array. – SeanJA Mar 24 '10 at 23:02
0

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:""}
Djumaka
  • 500
  • 4
  • 10
  • Oh no... smarty is still a thing in 2022? – SeanJA Jan 18 '23 at 05:45
  • 1
    @SeanJA well a lot decades-old code is still out there, doing its job, so ... sadly yes. Not speaking of people stilloving smarty by some unholy reason – Djumaka Jun 17 '23 at 05:52
-1

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
  • 10,234
  • 5
  • 32
  • 42