1. Post #1
    Free Gameservers
    darksoul69's Avatar
    August 2006
    3,004 Posts
    Does anyone know how to execute smarty variables (i.e. {$reply.name} ) inside of PHP code without it breaking?

    Edited:

    Figured it out :)
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Winner Winner x 2 (list)

  2. Post #2
    graymic's Avatar
    September 2005
    187 Posts
    Does anyone know how to execute smarty variables (i.e. {$reply.name} ) inside of PHP code without it breaking?

    Edited:

    Figured it out :)
    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);
    Will definitely tidy up your code a little more.

  3. Post #3
    Jaykin Bacon: Episode 3
    SteveUK's Avatar
    May 2005
    2,469 Posts
    Although you've figured it out. It might be worth while to mention.
    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.
    Reply With Quote Edit / Delete Reply Windows 8 United Kingdom Show Events Agree Agree x 7 (list)

  4. Post #4
    graymic's Avatar
    September 2005
    187 Posts
    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.
    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);
    			}
    			
    		}
    	
    	}
    }
    Just create an object of template and load up the files through some templating system

  5. Post #5
    Unmetered VPS
    Fizzadar's Avatar
    June 2010
    1,247 Posts
    {$debug} anywhere in the template. Gives a popup of all the vars/info for the page:

    http://www.smarty.net/docsv2/en/lang...tion.debug.tpl
    Reply With Quote Edit / Delete Reply Mac Canada Show Events Agree Agree x 1 (list)