What is wrong with this Smarty php templating code? - php

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

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}

Call smarty variable from html instead of php

HTML CODE:
<html><body>
{section name=a loop=$items}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
PHP CODE:
$pid = '12';
$items = $cbvid->get_channel_items($pid);
assign('items',$items);
This is perfectly working fine, with the integer 12 being my php code. However, I wanted to add the integer 12 and call it from the html code, but it didn't work.
I tried:
<html><body>
{section name=a loop=$cbvid->get_channel_items(12)}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
But it didn't work. How can I do it?
Just don't do it. That looks like if you would like to move business logic to representation layer - that is not what Smarty is used for. Prepare data beforehand, then give it to template.
But if you really want it to work, use foreach
<html><body>
{foreach from=$cbvid->get_channel_items(12) item=video}
{$video.title}
{include file="/directory/showstuff.html" video=$video}
{/foreach}
</body> </html>
The reason why it did not work with sections, was because you have no $items variable defined, still, you are trying to get value of it. This is where you need to use assign.
<html><body>
{assign var="items" value=$cbvid->get_channel_items(12)}
{section name=a loop=$items}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>
Still, I prefer foreach.

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}

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}

smarty pagination for nested {section}

Here is the Sample Code:
{section name="firstlevel" loop=$xxx}
<ul>
{section name="secondlevel" loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{sectionelse}
No values
{/section}
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
I have limited 10 records per page, but this smarty code not calling the include file pagination. Please suggest what wrong in it or give idea...
I'm not a Smarty guru but I believe "last" only returns true while in the section. This would probably be preferable anyway as there is little or no use showing pagination when there are no records.
{section name=firstlevel loop=$xxx}
<ul>
{section name=secondlevel loop=$xxx[firstlevel]}
<li>$xxx[firstlevel][secondlevel].values</li>
{/section}
</ul>
{if $smarty.section.firstlevel.last}
{include file="pagination.tpl"}
{/if}
{sectionelse}
No values
{/section}

Categories