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

Plugin to parse variable bit by bit?

 
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
phoenix.pt
Smarty Rookie


Joined: 09 Dec 2003
Posts: 8

PostPosted: Thu Dec 18, 2003 12:50 am    Post subject: Plugin to parse variable bit by bit? Reply with quote

<newbie alert!>
I have a php variable in my template that is a concatenation/combination of individual "0" and "1" that represent characteristics [on/off]...

Here's one example... variable $ccards stores the acceptance of 3 credit card types by some shop. The first bit is related with VISA, the second with Mastercard, and the 3rd with American Express... So, a $ccard = "101" means that shop accepts VISA (first 1), doesn't accept Mastercard (the second bit, 0), and accepts AmEx (the last bit, 1).

My problem is... I need to parse this string "101" in my template, and for each bit, show a different image. In the case of that example, I could show a green dot, a red dot, and a green dot...

Does anybody knows if there any plugin that do something like this? Or at least parses the string char by char?

Anyway, what would be considered a function like this? A function? A modifier? A output filter?

TIA
</newbie alert!>
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Dec 18, 2003 1:53 am    Post subject: Reply with quote

You can write a plugin for this. Try the manual and check the examples here at the forum and at the wiki to learn how to roll your own -- it is surprisingly easy.

If you REALLY had to do this in-template, you could with a little thought and the knowledge that php functions can be used as modifiers and that {section} can be used to emulate a for loop.

Assume that you have an indexed array with each of the desired types in order:[php:1:d31e1a7695]<?php
$smarty->assign('names', array('first', 'second', 'third'));
$smarty->assign('input', '101');
?>[/php:1:d31e1a7695]
Code:
{section name=i loop=$input|strlen}
    {assign var=current value=$input|substr:$smarty.section.i.index:1}
    {$names[$smarty.section.index]} is $current</br>
{/section}

Should produce:
Quote:
first is 1
second is 0
third is 1

This uses 2.6.0 syntax.

HTH
Back to top
View user's profile Send private message
phoenix.pt
Smarty Rookie


Joined: 09 Dec 2003
Posts: 8

PostPosted: Thu Dec 18, 2003 2:34 am    Post subject: Reply with quote

Thanks for the quick reply and simple explanation! Smile

I didn't know that section could receive php functions that way, and would never even think about that, because in my limited knowledge of smarty, foreach and section = parse arrays, only... Smile

Anyway, if you say that writing a plugin is so easy, I'll surelly try to do so... I believe that you advise a function instead of this hack because of performance and/or security issues? Those 2 are very important to me, and I prefer to not include php code in the templates...

I'm also having another problem right now, and thats probably such a newbie question that I think it doesn't deserve another new topic...

I have some constants defined in a php config file, that I can usually access with {$smarty.const._THE_CONSTANT}. But now I need to "build" the constant name from a php variable, like this:

{$smarty.const.$var}, so if the $var = "_THE_CONSTANT", that would lead to {$smarty.const._THE_CONSTANT}, or if the $var = "TEST123", it would be {$smarty.const.TEST123}

Since you seem to be the "god" around here, do you have any ideia if this is possible, or how can I hack this situation? I read an old topic about var names and constant names, but it seemed to me in my little knowledge, that it was the "oposite" problem...
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Dec 18, 2003 2:47 am    Post subject: Reply with quote

I suggest a plugin mainly because it makes it easier for the designer to use the logic instead of having to use all of those php functions in the template every time this technique is required.

As for constants, they are a bit difficult in Smarty, IMO. I think you are on the right track. To get the vars you want, you can do something like:

{assign var=temp value=$firstpart|cat:$secondpart} or
{assign var=temp value="`$firstpart``$secondpart`} (see the manual of using vars in double quotes and also the cat modifier).

Then your syntax to access the constants should work.

By-the-way, I'm just a user too, like anyone else, but thanks for the compliment Smile

Greetings.
Back to top
View user's profile Send private message
phoenix.pt
Smarty Rookie


Joined: 09 Dec 2003
Posts: 8

PostPosted: Thu Dec 18, 2003 2:47 pm    Post subject: Reply with quote

You're right... Writing plugins for Smarty is easy (at least if they do something easy with php)! Smile

I tried my first modifier to adress the last problem, since none of other "in template" solutions were working...

Here's the code:

<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/

/**
* Smarty get constant with var name
*
* Type: modifier<br>
* Name: getconst<br>
* Date: Dez 18, 2003
* Purpose: get a constant value with a variable name
* Input: the var representing the constant name
* Example: {$var|getconst}
* @link http://smarty.php.net/manual/en/language.modifier.cat.php cat
* (Smarty online manual)
* @author Pedro Lopes <phoenix@virtualmind.ws>
* @version 1.0
* @param string
* @return string
*/
function smarty_modifier_getconst($string) {
return constant($string);
}
?>

Tested it and works fine (it's extremelly basic, anyway... Wink I write in template {$var|getconst} and if the var is "_SOME_NAME", this will return the value of the _SOME_NAME contant, if defined...

Two questions:
1) It's my first plugin... Do you see anything incorrect, or that doesn't comply with Smarty Plugin Rules?
2) Do you think I should share such simple plugins, or I should keep this to myself, and not bother people with such 2 line basic functions?

You're only a user? You seem to be everywhere! Smile Well, you've been a great help and gave me confidence to take a peek how the plugins work... Smile

Bet Regards,
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Dec 18, 2003 4:32 pm    Post subject: Reply with quote

Certainly it is possible to write functions/modifiers that can do just about anything, but IMHO there is a better way to approach this particular problem.

Splitting up that '010' string is business logic, that should not be handled in the templates. Do that work in your PHP application and assign boolean template variables as appropriate:

$show_visa_image
$show_mc_image
$show_amex_image

Then in the template:

{if $show_visa_image}<img src="visa.jpg">{/if}
{if $show_mc_image}<img src="mc.jpg">{/if}
{if $show_amex_image}<img src="amex.jpg">{/if}

With this approach there is no need for custom template functions/modifiers, and the templates are dealing with presentation elements only.

As for accessing a variable variable constant, same rule applies. Do the work in the PHP application to find what you are looking for and assign it to the template instead of trying to directly access the constant from smarty.
Back to top
View user's profile Send private message Visit poster's website
phoenix.pt
Smarty Rookie


Joined: 09 Dec 2003
Posts: 8

PostPosted: Thu Dec 18, 2003 7:51 pm    Post subject: Reply with quote

You're probably right, mohrt, and I usually like to keep things that way: smarty deals only with presentation, not data handling, but...

This particular var is not alone... I get it from a MySQL db, along with other fields, all retrieved and processed with the php script (also using ADODB and a few "personal" classes). For this particular example, I query the db about some particular data, get a variable number of items, and send it to smarty in a structure more or less like this:

$items = Array (
...[0] => Array ( <---- This is the first item retrieved
......[0] => "name",
......[1] => "city",
......[2] => "ccards", <--------This is the example field, like "010"
......[n] => ...other fields... ),
...[1] => Array ( <---- This is the second item retrieved
......[0] => "name",
......[1] => "city",
......[2] => "ccards", <--------This is the example field, like "010"
......[n] => ...other fields... ),
...[n] => Array ( <------- And so on...
......[0] => "name",
......[1] => "city",
......[2] => "ccards", <--------This is the example field, like "010"
......[n] => ...other fields... ),
...); (dots are for spacing)

I know how to parse and present all the fields data of every item found sequentially and organized, but only with php. What I am doing with smarty, is to send this array to the template, and then parse it there with a:

{foreach from=$items item=it}
<b>{$it[0]}</b> {$it[1]} - {$it[5]}
{/foreach}

or something like that... Is there a better way of doing this, not knowing how many items the query will return, but considering that it could be 100's? My main concerns are with security and performance, since it should be prepared for a big load... Thats why I'm caching queries with ADODB, and caching templates with Smarty (I'll also use css for presentation).

You probably allready noticed my small experience with PHP, and the almost null knowledge of smarty/ADODB or maybe even programs in general, but... I research a lot, read much, and make many (stupid?) questions like this... Smile Sorry! Very Happy
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Dec 18, 2003 8:19 pm    Post subject: Reply with quote

You could munge the array in PHP to look like this:

$items = Array (
...[0] => Array (
......[0] => "name",
......[1] => "city",
......[2] => "show_visa",
......[3] => "show_mc",
......[4] => "show_amex",
......[n] => ...other fields... ),

Or, if you don't want to change the number of array elements:

$items = Array (
...[0] => Array (
......[0] => "name",
......[1] => "city",
......[2][0] => "show_visa",
......[2][1] => "show_mc",
......[2][2] => "show_amex",
......[n] => ...other fields... ),


The point is, do your array slicing, etc. in PHP and pass data to the template that is ready for presentation. Try to avoid having to do further data deciphering in the template to determine how to display something. That should already be done.
Back to top
View user's profile Send private message Visit poster's website
phoenix.pt
Smarty Rookie


Joined: 09 Dec 2003
Posts: 8

PostPosted: Wed Dec 31, 2003 4:19 pm    Post subject: Update: Thanks! Reply with quote

Just to inform you both that I rewrited my script to follow your guidelines, and the result seems much better organized and faster... Cool

Thanks for pointing me the right direction! Smile
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