Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Assigned variable as modifier and it's parameters

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
anorgan
Smarty n00b


Joined: 18 Oct 2008
Posts: 4

PostPosted: Sat Oct 18, 2008 11:53 pm    Post subject: Assigned variable as modifier and it's parameters Reply with quote

I've been looking for the answer to variabile modifiers, and couldn't find it on the net, so after an hour of digging I came up with this. Lets say you have a need to make a list of items stored in a table. You could have a table with id, title, body, date and so on. After you pull out your data, and assign it to smarty, you can easily make a table as here:

Code:

<table cellspacing="2" cellpadding="2" bsort="2" width="100%">

<tr>
    {section name=header loop=$fields}
       <td>{$fields[header].name}</td>
    {/section}
</tr>

{section name=entity loop=$entities}
<tr>
{foreach from=$fields item=field}
    {assign var='cell' value=$field.field}

    {if $field.modifier != ''}
       {capture assign=test}{ldelim}$entities[entity].$cell|{$field.modifier}{if $field.parameter}:"{$field.parameter}"{/if}{rdelim}{/capture}
    <td>{eval var=$test}</td>
    {else}
    <td>{$entities[entity].$cell} </td>
    {/if}

{/foreach}
</tr>
{/section}
</table>


You need an array to know what fields you have in the table, so the php side looks like:
Code:

$entities = $db->fetch("SELECT * FROM $entity_table ORDER BY $order_by $sort LIMIT $offset, $limit");
$smarty->assign('entities', $entities);

$fields = array(
array('field' => 'name',  'name' => 'Title'),
array('field' => 'alias',    'name' => 'Alias'),
array('field' => 'info',     'name' => 'Description'),
array('field' => 'date',    'name' => 'Date', 'modifier' => 'date_format', 'parameter' => '%d.%m.%Y')
);
$smarty->assign('fields', $fields);


After parsing the template, smarty doesn't know what to do with dates, so leaves them in 2008-10-19 format. That is why we tell him what modifier and parameters to use for which table cell. And the magick happens here:

Code:

{capture assign=test}
{ldelim}$entities[entity].$cell|{$field.modifier}{if $field.parameter}:"{$field.parameter}"{/if}{rdelim}
{/capture}

{eval var=$test}


You capture the whole string and eval it later, so the string you want assigned via capture is {$entities[entity].cell|date_format:"%d.%m.%Y"}, and afther the eval does it's thing, you have a clean date. Can be used without parameters for modifier, or without modifier.

Further modification to this would be to add array for modifiers, so you could do something like {$entities[entity].cell|uppercase|trim}, but I didn't have the need for it, so didn't develop it that way.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP