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

new plugin: str_pad

 
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
pabro
Smarty n00b


Joined: 20 Jun 2003
Posts: 3

PostPosted: Thu Jun 03, 2004 3:55 pm    Post subject: new plugin: str_pad Reply with quote

- ptBR -
dae povo..
eu adicionei um plugin (modificador), chamado str_pad, la' no wiki.
eu nao sabia como fazer algo como 'str_pad' na smarty, entao... eu criei isto.
viva meu portugues! = )


- en -
hi folks..
i've added a plugin (modifier), called str_pad, in wiki.
i didn't know how to do something like 'str_pad' with smarty, so.. i've made this.
sorry my english.

Code:

<?php
 /*
  * Smarty plugin
  *
  * Type:     modifier
  * Name:     str_pad
  * Date:     Jun 02, 2004
  * Version:  1.0
  * Author:   Pablo Dias <pablo at grafia dot com dot br>
  * Purpose:  pad a string to a certain length with another string. like php/str_pad
  *
  * Example:  {$text|str_pad:20:'.':'both'}
  *    will pad $string with dots, in both sides
  *    until $text length equal to 20 characteres
  *    (assuming that $text has less than 20 characteres)
  *
  * Input:
  *    string - the string to be padded
  *    length - desired string length
  *    pad_string - string used to pad
  *    pad_type - both, left or right
  */

function smarty_modifier_str_pad($string, $length, $pad_string=' ', $pad_type='left') {
  $pads = array('left'=>0, 'right'=>1, 'both'=>2);
  if(array_key_exists($pad_type, $pads))
    return str_pad($string, $length ,$pad_string,$pads[$pad_type]);
}

?>


falow
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


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

PostPosted: Thu Jun 03, 2004 9:32 pm    Post subject: Reply with quote

Hi pabro:

As this is basically just a wrapper for the PHP built-in function str_pad, you can typically use the function as a Smarty modifier:
Code:
{"abc"|str_pad:10:"-"}


You can even use the PHP constants, like so:
Code:
{"abc"|str_pad:10:"-":$smarty.const.STR_PAD_BOTH}


Of course, you would have to use a plugin like yours if you have Smarty security turned on and/or are limiting the PHP functions that can be called from a template, but otherwise, the above examples work just fine. Plugins that wrap builtins are also useful when you want to modify the behaviour of the builtin, use a different order of parameter passing or provide specific defaults/overrides for specific parameters.

Cheers!
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