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

template function with 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 -> Plugins
View previous topic :: View next topic  
Author Message
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Sun Nov 30, 2003 1:27 pm    Post subject: template function with parameters Reply with quote

Hello, I want to write in a smarty template something like this:

{"Submit"|button_length:8 }

So as "button_length" returns a string like this: ' Submit '. (If length was 10, one additional space should be added to the beggining and to the end of the string, do you understand me?)

I suppose that I have to design a "template function" but in the docs (http://smarty.php.net/manual/en/plugins.functions.php) there is not an example using parameters. I don't know how to do it.

Thanks in advance.
Back to top
View user's profile Send private message
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Sun Nov 30, 2003 4:12 pm    Post subject: Reply with quote

Ok, I am reading more docs and I have done the following:

- Make a file "modifier.spaces.php" and placed in /libs/plugins smarty directory.

- This files contains:

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: modifier.spaces.php
* Type: modifier
* Name: spaces
* Purpose: add spaces to the beggining and end of the text to
* obtain a string with the lenght requested
* -------------------------------------------------------------
*/

function smarty_modifier_spaces($sWord,$iSpaces){
$sResult = '';

$iLength = strlen($sWord);

if ($iLength<$iSpaces){
return $sResult;
}
if ($iLength==$iSpaces){
return $sWord;
}

$iTimes=($iSpaces - $iLength);

$iTimes1=($iTimes/2);
for($i=0;$i<$iTimes1;$i++)
$sResult .= ' ';

$sResult .= $sWord;

$iTimes2=($iTimes/2)+($iTimes % 2);
for($i=0;$i<$iTimes2;$i++){
$sResult .= ' ';
}

return $sResult;
}

But, if I use Smarty to call that function (for example, writing {'Submit'|spaces:10} then I receive the following error:

<br" />
<b>Fatal error</b>: Call to undefined function: spaces() in <b>/var/www/html/prodler/templates_c/%%206/%%2063100594/form_list.htm.tpl.php</b> on line <b>112</b><br />

And if I register the modifier on the PHP file that displays the TPL (using $smarty->register_modifier('spaces','spaces'); ), I receive the following error:

<br />
<b>Fatal error</b>: Smarty error: [in form_list.htm.tpl line 85]: [plugin] modifier 'spaces' is not implemented (core.load_plugins.php, line 36) in <b>/var/www/html/libs/Smarty.class.php</b> on line <b>1883</b><br />

Does anyone know what is my problem? Thanks in advance.
Back to top
View user's profile Send private message
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Mon Dec 01, 2003 12:13 pm    Post subject: Reply with quote

Oh dear, sorry for all who have been read the post. I have found my error:

- Using the first method (copying my file to Plugins directory) I forgot to end the file with the tag "?>". Now it works.

- Using the second method, I didn't include the file before registering the modifier!
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 -> Plugins 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