Smarty templates, MVC and variables inside templates - php

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.

Related

Shopware - Pass $sUserLoggedIn-Variable to Widget

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']));

codeigniter tpl {if} {else} documentation

I'm trying to modify an application with codeigniter..
but the if else loop etc.. is not the default if else ex.:
{if isset($form1)}
{/if}
{foreach $data_ as $row}
{/foreach}
{cycle value="odd,event"}
where I can find a tutorial to learn this php particularity in template?
thanks!

Can smarty block names be derived from smarty variables

I would like to define smarty block names according to smarty data, but I can't seem to do it.
Example:
{foreach $array as $code}
{block name=block_$code}
<div id='{$code}'></div>
{/block}
{/foreach}
My purpose is to extend a specific block_$code block by a child template. Is this possible or is there some other trick I could use to do this?
Thanks.
You can use the {assign} block and the cat modifier. For example
{foreach $array as $code}
{assign var=foo value="block_"|cat:$code}
{block name=$foo}
<div>
{/block}
{/foreach}
N.b. I've not tested this, but it should work. You might also be able to short-circuit this and just use {block name="block_"|cat:$code}.
I was able to find the following link from 2011 indicating that this wasn't possible at that time. I suspect it still isn't:
http://www.smarty.net/forums/viewtopic.php?t=19805&highlight=block%20variable%20name
The good news it that I was able to figure out how to make my code work without it. I wanted to be able to override just one of the divs defined by the foreach. Here's how I can do it:
Parent:
{foreach $array as $code}
{block name=code_loop}
<div>Normal Stuff</div>
{/block}
{/foreach}
Child:
{block name=code_loop}
{if $code == 'code of interest'}
<div>New Stuff</div>
{else}
{$smarty.block.parent}
{/if}
{/block}

How to check a complex condition in Smarty(PHP)

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}

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:""}
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}

Categories