PHP Smarty Insert functions and variables - php

I have this smarty template, I need to call a function from my file.
Everything works fine, php recognizes the parameters, but not the values of the smarty variables.
Code:
{insert name=file_details value=a assign=fd fid=$m[i].FID}
<ul class='list'>
{section name=x loop=$fd}
{insert name=gfs assign=afs a=$fd[x]}
<li><a href="{$baseurl}/files/{$fd[x].FID|md5}{$fd[x].s}/{$fd[x].fname}" target="_blank">
{$fd[x].fname} <b>[{$afs}]</b>
</a></li>
{/section}
</ul>
Meaning.
the first insert retrieves some file information from my database, using as parameter $m[i].FID everything works fine, when I tell him for example {$fd[x].fname} it displays the name of the file, but when I tell him {insert name=gfs assign=afs a=$fd[x]} and make a var_dump(a) in the function insert_gfs all the values appear as null.
Any help would be great, I don't know what i'm missing

Arrays are writen as $m.i in Smarty. Not $m[i].
If you replace all your array notations like this, it should work.
{insert name=file_details value=a assign=fd fid=$m.i.FID}
<ul class='list'>
{section name=x loop=$fd}
{insert name=gfs assign=afs a=$fd.x}
<li><a href="{$baseurl}/files/{$fd.x.FID|md5}{$fd.x.s}/{$fd.x.fname}" target="_blank">
{$fd.x.fname} <b>[{$afs}]</b>
</a></li>
{/section}
</ul>

Related

Assigning multiple classes to a tag using PHPTAL

I have an array of data I want to output as a UL using PHPTAL (easy) with class attributes supplied by the array (easy), a class attribute for first and for the last element (easy)... all at the same time (hard).
Ie. I want to combine:
<ul tal:repeat="item items">
<li class="${item/class}">${item/text}</li>
</ul>
with this
<ul tal:repeat="item items">
<li tal:attributes="class repeat/item/first 'first'">${item/text}</li>
</ul>
and this
<ul tal:repeat="item items">
<li tal:attributes="class repeat/item/last 'last'">${item/text}</li>
</ul>
This is purely presentational stuff, so I'd rather do it purely in PHPTAL. Is this possible? How?
No, there's no pure TALES for this.
<li tal:attributes="class php:repeat.item.last ? 'last'
: (repeat.item.first ? 'first' : NULL)">
This is a pretty old thread, but because no one mentioned it: an 'cleaner' way is probably through a custom modifier. (http://phptal.org/manual/en/split/custom-modifiers.html). Then you could have:
<li tal:attributes="class css-ordinal:repeat.item">
and as a benefit, you could reuse that in other elements, as it seems a pretty general idiom.

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

Is something wrong with wrapping whole Smarty templates in {strip} tags?

I want to keep my templates tidy and nicely intented but would like to deliver only very compact HTML to the browser.
Missing a better idea, I was wondering if there is something wrong with wrapping whole Smarty templates in {strip} tags like so?
{strip}
<div class="albums">
<h2>Alle Foto-Alben</h2>
<ul class="photos clearfix">
{foreach $albums as $album}
{if $album->publishedPhotos|count > 0}
<li>
<div>
<a href="album.php?id={$album->id}">
<img src="{$album->titlepic->thumb}" alt="{$album->titlepic->title}" />
<span class="title">{$album->title}</span>
<span class="counter">{$album->publishedPhotos|count} Foto{if $album->publishedPhotos|count != 1}s{/if}</span>
</a>
</div>
</li>
{/if}
{/foreach}
</ul>
</div>
{/strip}
It smells a bit unprofessional to me but I could not come up with something better.
One downside definitely is that you have to wrap every single one of your templates in those tags.
I'm happy to be corrected and would love to hear different approaches to keeping delivered code compact.
While it's not wrong i would suggest using a pre-filter instead. A pre-filter only runs when the template is compiling (so it doesn't slow down the server) and you don't need to wrap every template in {strip}. The following rows of code is taken from a project I am working on. It effectively strips most whitespace.
/* Minify the html */
function smarty_pre_minify($tpl_source, $smarty) { return preg_replace('/[ \t\n\r]+/s', ' ', $tpl_source); }
$smarty->registerFilter('pre', 'smarty_pre_minify');
Nope, there's nothing wrong with this.
Do it in a header/footer pair of templates, rather than wrapping each page individually.
Alternately, you could trigger the stripping on the PHP side of things.
Notice: Striptags will ignore includes. Free tip of the day. :-)

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}

Question about an array in gallery2

I'm working on a modification from a theme in the open source project "gallery2".
Here is the code that I am looking at:
For anyone familiar with it, its in the SearchShowAll.tpl file in the search module in the modules section of the download.
<ul class="giInfo">
{foreach from=$result.fields item=field }
<li>
<span class="ResultKey">{$field.key}:</span>
<span class="ResultData">{$field.value|default:" "|markup}</span>
</li>
{/foreach}
</ul>
It is a smarty foreach loop and it grabs this data
Title: BB 08 PR 6-340
Summary:
Keywords: A Hillbilly Cat; Gavin Jordan; Margo Hazell
Description:
Owner: Gallery Administrator
However, I just want it to get the Title: and display it, not any of the other stuff. I am unsure of what the array looks like and how to get a specific value from it in smarty format and also in the case of this gallery.
I used section tags to figure this out. I was referencing the array value wrong as well. Here is how I got it to work.
{section name=field loop=$result.fields max=1}
<li>
<span class="ResultData">{$result.fields[field].value|default:" "|markup}</span>
</li>
{/section}

Categories