Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Easy variable assignment

 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
c2c
Smarty n00b


Joined: 31 Dec 2004
Posts: 1
Location: Paris

PostPosted: Fri Dec 31, 2004 5:19 pm    Post subject: Easy variable assignment Reply with quote

You need PHP5 to use this trick

I use a cool way to assign my template's var, using PHP5' SPL.

I create an object which extends smarty and Implements ArrayAccess :

Code:

class Template extends Smarty implements ArrayAccess {
   /**
   * Indicate if an a var is assigned
   *
   * @param string $name Var name
   */
   public function      offsetExists($name) {
      return (isset($this->_tpl_vars[$name]));
   }

   /**
   * Get an assigned var's value
   *
   * @param string $name Var name
   */
   public function      offsetGet($name) {
      return ($this->_tpl_vars[$name]);
   }

   /**
   * Assign template's var
   *
   * @param string $name  Var name
   * @param mixed  $value Value
   */
   public function      offsetSet($name, $value) {
      $this->assign($name, $value);
   }

   /**
   * Unset template's var
   *
   * @param string $name  Var name
   */
   public function      offsetUnset($name) {
      if (isset($this->_tpl_vars[$name])) {
         unset($this->_tpl_vars[$name]);
      }
      return (true);
   }
}


From now, you can use Template like Smarty (assign, fetch, etc...)

AND

assign your vars like if Template was an array :

Code:

// Assign a var
$template["geeting"] = "Hello, bob !";
$template["style"] = "align: center";

// Un Assign a var
if (...) {
  unset($template["welcome_text"]);
}

// Verify if a var was assigned
if (! isset($template["welcome_text"])) {
  $template["welcome_text"] = $template["default_text"];
}





I like that Smile
_________________
Julien CROUZET aka c2c
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks 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