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}
Related
I am trying to rewrite the RSS Feed Parser PHP Code to Smarty. The Smarty will not work for me somehow. Can somebody help me out please?
<ul>
<?php foreach ($feed['items'] as $item): ?>
<li>
<?= $item['title']; ?>
</li>
<?php endforeach; ?>
</ul>
My Smarty Code Updated: 12/07/13
{foreach name=aussen item=$feed.items from=$item}
<li>
{$item.title}
</li>
{/foreach}
There are a lot of issues with your smarty code. First, and foremost, you should NOT have php logic and smarty formatting in the same file. You should assign smarty variables separately and then call smarty for formatting. However if you stick with your existing stuff, you'll need to take into account the following:
syntax of your foreach is wrong - the correct syntax is {foreach from=$variable item=$loop_var}
php variables are not automatically available in smarty - you'll need to add smarty {assign} directive with a combination of{php}echo $php_var{/php}` to get php variable to smarty.
syntax {php}$item['title']{/php} is invalid syntax for php and will result in syntax error.
On the whole, your approach is flawed. Smarty is good for a presentation (view) layer in an MVC app - and you're trying to combine everything into one file. Don't. If you are not sure how to set your application properly as MVC, then smarty is not for you.
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
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}
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}