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

apply a php function to a smarty var

 
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 -> General
View previous topic :: View next topic  
Author Message
juque
Smarty Rookie


Joined: 30 Apr 2003
Posts: 8

PostPosted: Wed Jul 02, 2003 7:13 pm    Post subject: apply a php function to a smarty var Reply with quote

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 Sad 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
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jul 02, 2003 8:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jul 03, 2003 9:19 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
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 -> General 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