|
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 |
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Wed Apr 14, 2010 3:20 pm Post subject: Code Decorating Plugin |
|
|
This tutorial can be called some advanced topic, with intermediate level of hardness, and has its own practicality. Here, I want to make sure that my code numbers, if look something like ST2010070015M, T201000001MF, B999900005011, L067874, I want to print the redable form of their components.
These codes can be composed of anything, and should be a part of the business logic.
To take an example, ST2010070015M is a code given to a student with these logics:
Code: | ST: Identifying prefix
2010: Four digits of the year of admission
07001: Five digits unique code of the student
5: One digit rating of the student’s skills
M: Gender |
Now, I want to split them into individual components, and colorize them – this is the theme of this “decorate” modifier. All other codes too have their own logic for compositions. However, the spirit of this modifier is on its regular expressions and switch case. In the switch block, 1 (one) is passed, while only one of the regular expressions match with one results – which looks complex, but not so.
File: modifier.decorate.php, within your plugins directory:
Code: | <?php
/**
* ID / Code Decorator Modifier
* Makes sure that a code is readable, and separates its composition.
*/
function smarty_modifier_decorate($code='')
{
$data = array();
$decorator = '';
switch(1)
{
# Student: STYYYNNNSG
case preg_match('/^(ST)([\d]{4})([\d]{5})(\d)([A-Z])$/', $code, $data):
unset($data[0]);
$data[1] = "<strong style=\"color:#006600;\">{$data[1]}</strong>";
$data[2] = "<strong style=\"color:#FF0000;\">{$data[2]}</strong>";
$data[3] = "<strong style=\"color:#000099;\">{$data[3]}</strong>";
$data[4] = "<strong style=\"color:#FF0000;\">{$data[4]}</strong>";
$data[5] = "<strong style=\"color:#006600;\">{$data[5]}</strong>";
break;
# Teacher: TYYYYNNNNNGT
case preg_match('/^(T)([\d]{4})([\d]{5})([A-Z])([A-Z])$/', $code, $data):
unset($data[0]);
$data[1] = "<strong style=\"color:#006600;\">{$data[1]}</strong>";
$data[2] = "<strong style=\"color:#FF0000;\">{$data[2]}</strong>";
$data[3] = "<strong style=\"color:#000099;\">{$data[3]}</strong>";
$data[4] = "<strong style=\"color:#FF0000;\">{$data[4]}</strong>";
$data[5] = "<strong style=\"color:#006600;\">{$data[5]}</strong>";
break;
# Book: BCCCCNNNNNPPP
case preg_match('/^(B)([\d]{4})([\d]{5})([\d]{3})$/', $code, $data):
unset($data[0]);
$data[1] = "<strong style=\"color:#006600;\">{$data[1]}</strong>";
$data[2] = "<strong style=\"color:#FF0000;\">{$data[2]}</strong>";
$data[3] = "<strong style=\"color:#000099;\">{$data[3]}</strong>";
$data[4] = "<strong style=\"color:#FF0000;\">{$data[4]}</strong>";
break;
# Lab Records: LSSSNNN
case preg_match('/^(L)([\d]{3})([\d]{3})$/', $code, $data):
unset($data[0]);
$data[1] = "<strong style=\"color:#006600;\">{$data[1]}</strong>";
$data[2] = "<strong style=\"color:#FF0000;\">{$data[2]}</strong>";
$data[3] = "<strong style=\"color:#000099;\">{$data[3]}</strong>";
break;
default:
$data = array($code);
}
return(implode('', $data));
}
?> |
Examples are:
Code: |
{$student_code|decorate}
{'ST2010070015M'|decorate}
{$book_code|decorate}
|
Result? Result is that the same code word will be published in colored forms, giving better eye capture. Using preg_replace, we can even shorten the code.
Let me know, if you can use this concept within your projects
Happy plugging in Smarty,
and Happy New Year 2067 (today), in Bikram Sambat. |
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Wed Apr 14, 2010 3:41 pm Post subject: Some Test Scripts |
|
|
Here is a PHP code, that tests a set of different Codes, if are handled by this plugin:
Code: | <?php
# Testing scripts
$codes = array(
'ST2010070015M',
'T201000001MF',
'B999900005011',
'L067874',
'Nothing',
);
foreach($codes as $c => $code)
{
$decorated = smarty_modifier_decorate($code);
echo("<div>{$code}: {$decorated}</div>");
}
?> |
|
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
|
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
|
|