• PHP and Smarty
    4 replies, posted
  • Does anyone know how to execute smarty variables (i.e. {$reply.name} ) inside of PHP code without it breaking? [editline]18th August 2012[/editline] Figured it out :)
  • Avatar of graymic
  • [QUOTE=darksoul69;37298726]Does anyone know how to execute smarty variables (i.e. {$reply.name} ) inside of PHP code without it breaking? [editline]18th August 2012[/editline] Figured it out :)[/QUOTE] Although you've figured it out. It might be worth while to mention. You better off creating a handler class for smarty. So you can do something like so: [code] $dataArray = array ( 'name' => 'Joe Blogs', 'email' => 'email@email.com' ); $this->template->display('template.tpl', $dataArray); [/code] Will definitely tidy up your code a little more.
  • [QUOTE=graymic;37300216]Although you've figured it out. It might be worth while to mention. [/QUOTE] Yeah, if someone finds out the solution themselves, they should actually post the solution. There's nothing more infuriating than looking for a solution on Google only to find the OP said they found the solution and didn't say how.
  • Avatar of graymic
  • [QUOTE=SteveUK;37300294]Yeah, if someone finds out the solution themselves, they should actually post the solution. There's nothing more infuriating than looking for a solution on Google only to find the OP said they found the solution and didn't say how.[/QUOTE] Sure. I'm not able to test this but, this should work. Hopefully, so ergo my original point something like this... [CODE] require_once(SMARTY_DIRECTORY . 'Smarty.class.php'); namespace Core { class Template { private $_smarty; private $_globalAssigns = array ( 'thedate' => time() ); function __construct() { $this->_smarty = new \Smarty(); $this->_smarty->compile_dir = SMARTY_COMPILE_DIR; // Define your compile directory. $this->_smarty->debugging = SMARTY_DEBUG; // Define a bool. $this->_smarty->cache_lifetime = SMARTY_CACHE_LIFETIME // Define how long you want to cache for. $this->_smarty->compile_check = SMARTY_COMPILE_CHECK // I usually set this to false. $this->_smarty->caching = SMARTY_CACHING // I set this to 0. (0,1). $this->_smarty->template_dir = SMARTY_TEMPLATE_DIRECTORY; // I would strongly suggest having a template folder. } /** * Pointless Alias functionality for smarty... * * @param String $key * @param Array $data * @return Void * */ private function _assign($key, $data) { $this->_smarty->assign($key, $data); } /** * Small function to load template files from within a define template directory. * * @param String $templatePath * @param Array $data * @return Void */ public function display($templatepath, $data) { // Assign the global template data. $this->_assign('globals', $this->_globalAssigns); // Assign the template specific data. if (isset($data)) { foreach ($data as $key => $value) { $this->_assign($key, $value); } } // Display the template. if ($templatepath) { $this->_smarty->display ($templatepath); } } } } [/CODE] Just create an object of template and load up the files through some templating system
  • Avatar of Fizzadar
  • {$debug} anywhere in the template. Gives a popup of all the vars/info for the page: [url]http://www.smarty.net/docsv2/en/language.function.debug.tpl[/url]