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

Create an array with Smarty - Plugin By Carlos Galindo

 
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
cjxxi
Smarty Regular


Joined: 03 Aug 2007
Posts: 40
Location: Fort Worth,Texas

PostPosted: Sun Sep 14, 2008 8:41 am    Post subject: Create an array with Smarty - Plugin By Carlos Galindo Reply with quote


Usage:



My plugins: Never need patches or special requirements, no hassle... Just paste it and use it.. and they all use the Zend framework syntax guidelines and php5 (that's how we roll)


Non-associate array:
Quote:

{array vars="array('hours','days','months')" assign="foo"}


{foreach from=$foo item=v}
--<b>{$v}</b><br />
{/foreach}

output:


--hours
--days
--months




Associate array:


Quote:

{array vars="array('hours' =>'Hours','days' =>'Days','months'=>'Months')" assign="foo"}

{foreach from=$foo key=k item=v}
--<b>{$k}</b> - <b>{$v}</b><br />
{/foreach}

output:

--hours - Hours
--days - Days
--months - Months






Hello,

I this is a new plugin which allows you to create an array inside smarty.

Notice that to create an array you would need to be consistent in weather your arrays is associate or not. It can create either one of the two, but not both at the same time mixed together (at the moment).
Any how, by this I mean..
Associate:
Code:
 array('key 1' => 'value 1','key 2' =>'value 2');

Or
Non- Associate:
Code:
 array('value 1' , 'value 2');






Here is the plugin

Code:
plugins/function.array.php




Code:

<?php
/**
 #   Date: September 14 2008
 #   Author:   Carlos Galindo
 #   Site:  cjax.net
 #   Email: cjxxi [- at -] msn.com
 #   Last modified:   September 14 2008 7:00 AM  GMT -6
 **/

function smarty_function_array($params, &$smarty)
{
   $array = (isset($params['vars'])?$params['vars']:null);
   if(!$array) {
      return;
   }
   $new_array = array();
   if(strpos($array,"=>") !==false) {
      preg_match_all("/(['|\"]*([a-zA-Z0-9_\s\t\n]+)['|\"]*\s*\=\>\s*['|\"]*([a-zA-Z0-9_\s\t\n]+)['|\"]*\s*)+/",$array,$arr,PREG_SET_ORDER);   
      foreach($arr as $k =>$v) {
         $new_array[$v[2]] =$v[3];
      }
   } else {
      preg_match_all("/(['|\"]*([a-zA-Z0-9_\s\t\n]+)['|\"]*\s*)+/",$array,$arr,  PREG_SET_ORDER);   
      unset($arr[0]);
      foreach($arr as $k =>$v) {
         $new_array[] =$v[2];
      }
   }
   if(count(array_keys($params))==2 && isset($params['assign'])) {
      $smarty->_tpl_vars[$params['assign']] = $new_array;
   }
}



Code:
should work with php4, but I recommend php5


Feedback is welcome Very Happy
_________________
CJAX THE PHP AJAX FRAMEWORK AT CJAX.NET
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Sep 14, 2008 2:48 pm    Post subject: Reply with quote

There is also the {set } compiler plugin in the wiki.
See http://smarty.incutio.com/?page=set
Back to top
View user's profile Send private message
washburn
Smarty n00b


Joined: 20 Dec 2010
Posts: 1

PostPosted: Mon Dec 20, 2010 11:18 am    Post subject: regexpr incorrect Reply with quote

Just so you know, your code is broken. A simple "/" in the value will mess up the array parsing.
Why don't you use "eval()" ? Since the value is in the code there is no risk of malicious code injection.

Code:
   $array = (isset($params['vars'])?$params['vars']:null);
   if(!$array) {
      return;
   }

   eval ('$new_array = '.$array.';');

   if(count(array_keys($params))==2 && isset($params['assign'])) {
      $smarty->_tpl_vars[$params['assign']] = $new_array;
   }
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