|
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 |
juque Smarty Rookie
Joined: 30 Apr 2003 Posts: 8
|
Posted: Wed Jul 02, 2003 7:13 pm Post subject: apply a php function to a smarty var |
|
|
hi everyone.
I have this Smarty code:
Code: |
<select>
<option value="" selected>--seleccione el documento--</option>
{foreach key=k item=item from=$documentos}
<option value="{$item}">{php} echo basename("{$item}") {/php}</option>
{/foreach}
</select>
|
BUT when I run my template the Item Label of the <option> return empty.
What am I doing wrong?
TIA _________________ .:: JUan Pablo AqueveQUE ::. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 02, 2003 8:20 pm Post subject: |
|
|
Quote: | Code: | <option value="{$item}">{php} echo basename("{$item}") {/php}</option> |
|
Why embed the PHP echo in the {php} .. {/php} section? Ahh, you want to use the basename function. Create the basename function as a Smarty plugin function (or modifier).
I do not believe that you can use the {$item} within your PHP block as you have it listed. Further, $item will not be available to your inlined PHP code. If you really want to do it this way, it should read:
Code: | <option value="{$item}">{php} echo basename($this->get_template_vars('item'); {/php}</option> |
Not nice, huh? If you write it as a plugin, it might look like this:
Code: | <option value="{$item}">{basename name=$item}</option> |
I think a good rule of thumb is to avoid inlining PHP code in templates, whenever possible. I hold that to be true even if it implies creating a new custom plugin. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jul 03, 2003 9:19 pm Post subject: |
|
|
simply do {$item|basename} .
if you have $smarty->security=true you have to register "basename" as an allowed modifier (see the docs how to do that), otherwise (which is the default) every php-function can be used as a modifier. |
|
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
|