Access smarty variables: smarty 3.1.12 - php

I am using smarty 3.1.12 for templating, however in my php files i am tryig to access my smarty variables. All my research has returned answers only for previous versions of smarty.

use below code
$myVars = $this->get_template_vars();
$this->assign('myVars',$myVars);
{foreach from=$myVars key=k item=i}
{$k}: {$i}
{/foreach}

in php file:
$smarty->assign('myVars',$myVars);
in tpl file:
{$myVars}
or:
{foreach $myVars as $k=>$var}
{$var}
{/foreach}

$myVar = $SMARTY->getTemplateVars('myVar');
Use this method for your smarty version 3.1.12

Related

How to print the content of an array in phpfox

I'm working on a legacy application that is written in PHPfox.
There is this foreach loop in one of my view.html.php files:
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
<td>{$aReportSubmission.event_name}</td>
{/foreach}
The problem is that $aReportSubmission.event_name is an array and it gets outputted as Array. How can I loop through this variable and show the individual events?
PHPfox uses template tags that resemble Smarty template engine.
The following did NOT work for me:
{foreach from=$aReportSubmission.event_name item=iEvent} {iEvent},{/foreach}
I can see $ is missing before iEvent inside the inner loop
Check if this is works
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
<td>{foreach from=$aReportSubmission.event_name item=iEvent} {$iEvent},{/foreach}</td>
{/foreach}
Try with this
{foreach from=$aReportSubmissions key=iKey item=aReportSubmission}
{$aReportSubmission.event_name|print_r}
{/foreach}

Smarty templates, MVC and variables inside templates

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.

What is wrong with this Smarty php templating code?

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

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