Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Recursive function on each assigned template variable ?

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Fri Sep 13, 2013 9:37 am    Post subject: Recursive function on each assigned template variable ? Reply with quote

Hello everyone,

I'm currently getting bald working with Smarty and DomPDF to create templates for PDF output, since I have to deal with UTF-8 and ANSI.

Well, some TPL vars (strings) that aren't correctly displayed can be fixed by the following method of my controller :
Code:

public static function FixCharset($String) {
   return htmlspecialchars_decode(htmlentities($String, ENT_NOQUOTES, 'ISO-8859-1', false), ENT_NOQUOTES);
}


Now, I can register it and use it on every string :
Code:

// php
$this->Smarty->RegisterPlugin('modifier', 'FixCharset', Array(__CLASS__, 'FixCharset'));

// smarty
{$Address.addressStreet|FixCharset|escape}


OK, that works fine.
Now, I want to clean my tpl files since FixCharset is not an exception to apply to a few variables, but a rule to apply to each f*ck*n string that is assigned to the template, no matter if the string is accessible directly, from an array, an array of arrays, an object, etc. Then I wouldn't have to invoke the "|FixCharset" modifier on each Smarty variable.

So, I did a recursive method to do the job :
Code:

public static function FixCharsetRecursive($Var) {

   if (is_array($Var)) :
      return array_map(Array(__CLASS__, 'FixCharsetRecursive'), $Var);
      
   elseif (is_object($Var)) :
      foreach ($Var AS &$Value)
         $Value   =   self::FixCharsetRecursive($Value);
      return $Var;
      
   elseif (is_numeric($Var)) :
      return $Var;
      
   endif;
      
   return self::FixCharset($Var);
}


Actually, I want to apply this method on $this->Smarty->get_template_vars(), and not on the HTML output.

Is there a way to do this ?

I'm using Smarty 3.1.14.

Thanks for your advices,
Ben
Back to top
View user's profile Send private message Visit poster's website
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Fri Sep 13, 2013 1:02 pm    Post subject: Reply with quote

It's OK, I did it that way :

Code:
$this->Smarty->tpl_vars   =   self::FixCharsetRecursive($this->Smarty->tpl_vars);


Not really clean since tpl_vars is public, but it works...
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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