Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Wrtiing intermediate level Smarty Modifier Plugin

Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions
View previous topic :: View next topic  
Author Message
Smarty Elite

Joined: 19 Apr 2007
Posts: 420
Location: Kathmandu, Nepal

PostPosted: Mon Jun 08, 2009 6:55 pm    Post subject: Wrtiing intermediate level Smarty Modifier Plugin Reply with quote

This tutorial is only for beginers and intermediate users.
Please prvovide me a feedback on quality of the article and usefulness of this plugin.
Here, I write a step-by-step guide to writing a Smarty Modifier Plugin.

- Bimal Poudel

Normally, we store minified or coded parts of most frequently occuring contents in the database.
For example, we may write 1 for the text, First, ... and so on.
In an academic grades, p may mean, pass, f may mean fail.
Similary, F may mean femane and M may mean Male.

Okay, forget this here now.

The purpose of this short article is to make you able to write some intermediate level
Smarty plguins. And, the examples above are just a platform to start with the problem.

Again, please note that, checking the database for converting the meaning of each
encoded chunk of data is too costly for webserver in terms of memroy and performance.

If we can determine that there are very limited number of items to convert, adding a
little php function to show the full-form of the data would be a nice trick, instead
of the database lookup. And, Smarty can support this behaviour by writing a modifier

Never worry about what are they, if you have NOT used them before.
Once, look into the folder: smarty/lib/plugins, where you can see a list of files as:

Each of the files you see there are the different kinds of Smarty plugins, that probably
came with normal distribution of Smarty.

However, there IS NOT something like that can convert 'F' into Female or anything else.
We need to write them ourseleves, as requried in getting the proper solutions for our

Ok, we are going to add one more, like these files.
Please make a file named: modifier.decode.php in this folder, and put the contents

* Decodes different values - as were queried from the database, directly.
function smarty_modifier_decode($decode_value=0, $decode_for='gender')
   $decoded = '';
   $decodes = array();

   * We should not waste server memory.
   * Hence, we are initializing the variables only after we are sure that we have something to decode.
      * This is a multipurpose decoder.
      * First dimension of the array is the domain of the conversion.
      * Second dimesion of the array is the actual value to be decoded in the domain.

      * Decodes a gender
      $decodes['gender']['M'] = 'Male';
      $decodes['gender']['F'] = 'Female';
      * Decodes academic grades.
      * Please note that the indices are strings, not numeric.
      $decodes['grade']['1'] = 'Unacceptable';
      $decodes['grade']['2'] = 'Poor';
      $decodes['grade']['3'] = 'Acceptable';
      $decodes['grade']['4'] = 'Excellent (60% - 70%)';
      $decodes['grade']['5'] = 'Above 80%';
      $decodes['grade']['6'] = 'Over';

      $decoded = !empty($decodes[$decode_for][$decode_value])?$decodes[$decode_for][$decode_value]:"-- error decoding {$decode_value}:{$decode_for} --";


Save the file, and you are done writing a new Smarty modifier plugin.
It is of intermediate level programming. It does not have any hidden complexities,
and the source code most probably tells, what it is going to do with.

This plugin is a multi-purpose in usage. It can convert the gender codes and
grade codes, using a common programming. You might have understood this already.

If you say, decode for = gender, and send 'F', it gives you 'Female'.
And, if you say, decode for = grade and send '3', it gives you 'Acceptable'.

I wrote this plugin for a small realtime usage and for training purpose.
So, I have NOT cared the issues of security in this programming.
For example, I have NOT fully supporting, decode grade for 100.
It just yells a common message only, in this case.

Now, come back to your Smarty template, where you work most frequently for data
presentation. Add some nice codes as below, in a prominent part of the codes in .tpl file:

Compile this file. And, you see: Above 80%.

Anything within { and } is normally a Smarty tag.
5 - is some fixed number.
Pipe - initiates a modifer function.
decode - calls the smarty_modifier_decode function we just wrote.
Again, the FIRST paramter in this function is 5.
Because, 5 is being modified here.
Never get confused.
grade - This selects 'decode for' in our modifier function.
Finally, the overall = {5|decode:'grade'} = prints out the result of:
smarty_modifier_decode(5, 'grade'), ultimately means, 'Above 80%'.

Now, for some advanced usage, you can later write something like:
Here, $grade[g].n may be a variable within smarty/section.
Here, it is a real example from one of my project's source code.

Thank you for time in learning to write Smarty Modifer function.
I love Smarty all the times, and it has been at least 4 years I am in.

Happy modifying variables.

Last edited by bimal on Wed Jun 10, 2009 7:30 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Smarty Pro

Joined: 06 Feb 2009
Posts: 114
Location: India

PostPosted: Tue Jun 09, 2009 7:18 am    Post subject: Thanks for urs... Reply with quote

Just i have been here..
Nice tutorials for the beginners just like me....
Back to top
View user's profile Send private message Send e-mail
Smarty Pro

Joined: 06 Feb 2009
Posts: 114
Location: India

PostPosted: Tue Jun 09, 2009 7:20 am    Post subject: Reply with quote

Better post some nice example./...
Back to top
View user's profile Send private message Send e-mail
Smarty Regular

Joined: 28 Jan 2009
Posts: 60

PostPosted: Mon Jun 15, 2009 5:57 pm    Post subject: Reply with quote

I could definitely see this coming in handy.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions 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