|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
Posted: Wed May 14, 2003 3:04 pm Post subject: How to check an Array-value... |
|
|
Hi!
Smarty is and should not be as powerful as PHP itself, but now I have a problem:
In my php-script I do:
Code: | $tpl->assign('all', $db->getAll('SELECT...')); // PEAR::DB
$tpl->assign('users', $_SESSION['users']);
$tpl->display('lt_tabelle.tpl'); |
in my template-File:
Code: | {foreach from=$all item="row"}
<tr>
<td align="center" valign="top">&&
<input type="checkbox">
</td>
<td>{$row->user_id}</td>
<td>{$row->tel}</td>
<td><a href="#">{$row->email}</a></td>
</tr>
{/foreach} |
That works fine, but I want to have the checkbox checked, if the user_id of a row is a value of $_SESSION['users'], which is something like: array(1,5,99...);
In my template I can access it by {$users}, how could I build this in? I must build in {if} into the <input> tag, but what would be the right if-expression?
Or how could I change the variables in PHP so I can use something like
Code: | {if $users.$row->user_id} checked{/if} |
The problem is that I want to give back the data from database(all) as objects, so I cannot write in it anymore from PHP.
Any ideas?
regards
Andreas
Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 14, 2003 4:00 pm Post subject: |
|
|
If I got you correctly, you can write a modifier to wrap PHP's in_array. Then use your modifier to check for values in a given array. |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
Posted: Wed May 14, 2003 4:20 pm Post subject: |
|
|
You think of something like:
Code: | $smarty->register_modifier("contains","in_array"); |
But I need 2 parameters for in_array(), how can I do this?
Would be nice if I could assign the array to the modifier in PHP, so that I can use it in my templates like:
is this possible?
Thank you!
________
extreme vaporizer review
Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 14, 2003 4:58 pm Post subject: |
|
|
yes: see the manual on plugin modifiers. For example, here's a super simple LINK modifier that accepts 4 parameters:
Code: | function smarty_modifier_link($title, $href='', $id='', $target='')
{
if ( !empty($target) ) {
$_id = (empty($id)) ? '' : 'id="'.$id.'" ';
$__id = (empty($id)) ? '' : "#$id";
return '<a '.$_id.'target="'.$target.'" href="'.$href.$__id.'">'.$title.'</a>';
}
if ( !empty($id) ) {
$_id = (empty($id)) ? '' : 'id="'.$id.'" ';
$__id = (empty($id)) ? '' : "#$id";
return '<a '.$_id.'href="'.$href.$__id.'">'.$title.'</a>';
}
if ( !empty($href) ) {
return '<a href="'.$href.'">'.$title.'</a>';
}
return '<a>'.$title.'</a>';
} |
eg:
{"My Site"|link:"myreference.php":"myid":"_blank"}
I'd give you an example more specific to your task, but I'm in a rush at the moment and anyhow, it makes a good excercise--modifiers rock! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed May 14, 2003 5:00 pm Post subject: |
|
|
{if $var|in_array:$array}... works
you don't even have to register a modifier for that.
if you want opposit order, you have to write a modifier to wrap it:
modifier.contains.php:
Code: |
function smarty_modifier_contains($haystack, $needle) {
return in_array($needle, $haystack);
}
|
but be careful to call it
{if $array|@contains:$var}...
(note the @!) without the @ the modifier isn't called for $array but for each element of $array, which is not what you want.
the order is a matter of taste. i think "contains" reads nicer than "in_array", but the "@" is not nice (and easily forgotten) and not necessary for "in_array"
HTH
messju |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed May 14, 2003 6:38 pm Post subject: |
|
|
yes, that looks smartest! let php be the workhorse and keep the template free from function-calls and stuff. |
|
Back to top |
|
|
|
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
|
|