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:""}
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}
Related
i am currently having a problem with requesting if a user is logged in, in a Shopware-Widget.
I need to do this since some Elements on the Emotion-Pages should be hidden if the user is not logged in. What i just did was to alter the code in the component_html.tpl to something like this:
{if $Data.cms_title|substr:0:1 == "_"}
{assign var="private" value=true}
{else}
{assign var="private" value=false}
{/if}
{if $Data.cms_title}
<div class="page-header">
<h3>{$Data.cms_title|substr:1}</h3>
{if $private and $sUserLoggedIn}<p>This block is private</p>{/if}
</div>
{/if}
{$Data.text}
Unfortunately Shopware does not pass the template-variables (in this case $sUserLoggedIn) to the widgets by default, and i have no clue how to solve the problem.
Any help is appreciated :)
regards
The best way is to inject the DI Container to your class.
After that you have access to the Sesson via : $session = $this->container->get('session');
Now you can do something like that :
$view->assign('isUserLoggedIn', !empty($session->sUserId)); or
$view->assign('isUserLoggedIn', !empty($session->offsetGet("sUserId"));
Did you try to make $sUserLoggedIn a global variable? You can find a tutorial in the Shopware docs.
This is ugly, but shopware does it in it's own code. Well, with shopware almost everything looks like a hack.
In my plugin's bootstrap I do:
$view->assign('isUserLoggedIn', !empty(Shopware()->Session()['sUserId']));
I wanted opinion is it a good practice to do things like this while working with MVC architecture:
{foreach from=$items item="list"}
{if $list.index < 5}
{assign value="good" var=$class_name}
{else}
{if $list.index % 2 eq 1}
{assign value="bad" var=$class_name}
{else}
{assign value="average" var=$class_name}
{/if}
{/if}
{/foreach}
Or should I do such things inside php and then just access with:
{foreach from=$items item="list"}
{$list.class_name}
{/foreach}
The reason I'm asking this is, because I was told it's need to be done inside templates(because it's styling issues and etc), but I think opposite, I think it needs to be done inside PHP controller so that way you leave templates a little bit cleaner.
So what is the better approach and why?
If you referring class_name as in the <a **class="class_name"**>, then yes, You should generate that in the smarty template so that the template has control over how it should look. If you referring class_name to something else that is class Class_name, and you want to print that out. Then no, it should stay within your PHP code.
But all these are neither best practices stated anywhere or documented, but more company standard or team lead preferences. And you do what they want if you want to keep your job smooth and easy.
in a tpl file, there are code as this:
{$LANG.globalyouarehere}: {$breadcrumbnav}
now, i want to do if the url is https://example.com/1.php.it will add a welcome text before the {$breadcrumbnav}. if others, it will not add it.
the following is my ways, but it doesn't work and how to correct it?
{if $_SERVER['REQUEST_URI']==‘1.php’}welcome {$breadcrumbnav} {else}{$breadcrumbnav} {/if}
i got it. put this {$smarty.server.REQUEST_URI}
try this
{if $_SERVER['REQUEST_URI']==‘/1.php’}welcome {$breadcrumbnav} {else}{$breadcrumbnav} {/if}
What is wrong with this Smarty php templating code? If I include either of these two loops individually in the file, they work. But if I have them both in the file like below only the first loop gets completed.
{include file="vote_js.tpl"}
{section name=i loop=$posts}
{include file="posts_bitother.tpl"}
{/section}
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
So for example
{section name=j loop=$posts2}
{include file="posts_bitposts.tpl"}
{/section}
works fine on it's own.
Why is this happening and what is the way around it?
Solved it, was an error in my smarty code later on
I need to display a section or another in a smarty template. My condition is simple: if a smarty value starts with a string I should display one section, otherwise the other smarty section should be displayed. I can change only the tpl files.
{php}
if (substr($url,0,4) != 'http')
{
{/php}
section 1
{php}
}
else
{
{/php}
section 2
{php}
}
{/php}
The problem is that I can not read the url varible which was previously assigned using $smarty->assign. Basically, I'm looking for the smarty function that can be used to retrieve a value, or if there is a better solution.
First, I would clean up your code. You don't need php tags, you're using smarty:
{if substr($url,0,4) neq 'http'}
section 1
{else}
section 2
{/if}
That's untested but it should be pretty close..
Now, if you're trying to read something like a constant, for example a server variable like HTTP_HOST, you can do something like this:
{assign var='url' value=$smarty.server.HTTP_HOST}
{if substr($url,0,4) neq 'http'}
section 1
{else}
section 2
{/if}