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

"Proper" Array Support

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
balupton
Smarty Rookie


Joined: 15 Feb 2008
Posts: 11
Location: Perth, Australia

PostPosted: Sat May 03, 2008 10:24 am    Post subject: "Proper" Array Support Reply with quote

I've made a few changes in smarty to make the following possible.
Code:
   {* Set up pages: employee *}
   {assign var='pages' value='[]'}
   {assign var='pages.employee' value='[]'}
   {assign var='pages.employee.shift' value='Record a Shift'}
   {assign var='pages.employee.leave' value='Submit Leave'}
   {assign var='pages.employee.details' value='Edit your Details'}
   {assign var='pages.employee.timesheet' value='View your Timesheet'}
   {if $user.is_manager}
      {assign var='pages.employee.employees' value='Manage Employees'}
   {/if}
   <div class="actions employee">
      <ul class="inline selectable">
      {foreach from=$pages.employee key=key item=value name='menu'}
         {cycle values='odd,even' assign='class'}
         {if $smarty.foreach.menu.first}
            {assign var='class' value=$class|cat:' first'}
         {elseif $smarty.foreach.menu.last}
            {assign var='class' value=$class|cat:' last'}
         {/if}
         {if $key == $page}
            {assign var='class' value=$class|cat:' selected'}
         {/if}
         <li class="{$class}"><a href="index.php?page={$key}">{$value}</a></li>
      {/foreach}
      </ul>
   </div>
   {if $user.is_admin}
   {* Set up pages: admin *}
   {assign var='pages.admin' value='[]'}
   {assign var='pages.admin.settings' value='Edit Settings'}
   {assign var='pages.admin.employee' value='Add a Employee'}
   {assign var='pages.admin.department' value='Add a Department'}
   {assign var='pages.admin.worktype' value='Add a Work Type'}
   <div class="actions admin">
      <ul class="inline selectable">
      {foreach from=$pages.admin key=key item=value name='menu'}
         {cycle values='odd,even' assign='class'}
         {if $smarty.foreach.menu.first}
            {assign var='class' value=$class|cat:' first'}
         {elseif $smarty.foreach.menu.last}
            {assign var='class' value=$class|cat:' last'}
         {/if}
         {if $key == $page}
            {assign var='class' value=$class|cat:' selected'}
         {/if}
         <li class="{$class}"><a href="index.php?page={$key}">{$value}</a></li>
      {/foreach}
      </ul>
   </div>
   {/if}


Just replace the Smarty class's assign function (line 574) with
Code:
    /**
     * assigns values to template variables
     *
     * @param array|string $tpl_var the template variable name(s)
     * @param mixed $value the value to assign
     * @param mixed $var to use, for recursive arrays
     *
     * @author Monte Ohrt <monte at ohrt dot com>
     * @author Andrei Zmievski <andrei@php.net>
     * @author Benjamin "balupton" Lupton <contact@balupton.com> - {@link http://www.balupton.com/}
     *
     * @version 2.6.20
     */
    function assign($tpl_var, $value = null, &$var = null)
    {
       // Prepare var
       if ( $var === null ) $var = &$this->_tpl_vars;
      
      // Convert value to array if need be
      if ( $value === '[]' )
         $value = array();
      
      // Assign
        if ( is_array($tpl_var) )
      {   // We have a array to assign
            foreach ($tpl_var as $key => $val)
         {   // Cycle through array values, and assign each
                if ( !$key ) continue; // no reason to assign this
            if ( is_array($val) )
            {
               if ( !isset($var[$key]) ) $var[$key] = array();
               $this->assign($val, null, $var[$key]);
            }
            else
               $var[$key] = $val;
            }
        } else {
            if ( !$tpl_var ) return; // no reason to assign this
         // Check for array
         $parts = preg_split('/\\.|\\-\\>/', trim($tpl_var,'\'') );
         $parts_n = sizeof($parts);
         if ( $parts_n > 1 )
         {   // We have heirachy
            $eval = '$var[\''.implode('\'][\'', $parts).'\'] = $value;';
            eval($eval);
         }
         else
         {   // Basic var
                 $var[$tpl_var] = $value;
         }
        }
    }
Back to top
View user's profile Send private message Send e-mail Visit poster's website
joshspivey
Smarty n00b


Joined: 17 Jul 2008
Posts: 1

PostPosted: Thu Jul 17, 2008 8:43 am    Post subject: Curious Reply with quote

what exactly does your mod change? I think there should be away to do a print_r using smarty tags

Josh Spivey
It department
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 -> Feature Requests 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