Browse | Submit New Snippet | Create Package

 

Smarty View class

Type:
Full Script
Category:
Other
License:
MIT/X Consortium License
Language:
PHP
 
Description:
This is a simple view class that will allow you to use smarty with CakePHP.

Versions Of This Snippet::

Zoran Vučina
Snippet ID Download Version Date Posted Author Delete
4051.0.0.72010-07-03 07:56Zoran Vučina
Changes since last version::
ONLY FOR CakePHP 1.3
/*************************/
$paths = App::path('views');

foreach($paths as $path) {
if (file_exists($path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext)) {
$layoutFileName = $path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext;
return $layoutFileName;
}
}

// added for .ctp viewPath fallback
foreach($paths as $path) {
if (file_exists($path . 'layouts' . DS . $type . $this->layout . '.ctp')) {
$layoutFileName = $path . 'layouts' . DS . $type . $this->layout . '.ctp';
return $layoutFileName;
}
}

/*************************/
if (isset($this->webservices) && !is_null($this->webservices)){
3891.0.0.62008-09-17 18:59John Nelson
Changes since last version::
-- fixed Warning (512): (vendor) Deprecated, see App::import('Vendor', '...'); [CORE/cake/basics.php, line 1123]
---- replaced depreciated vendor() call with App::import()
3671.0.0.52008-02-11 15:28Kien Pham
Changes since last version::
- fixed the warning : Call-time pass-by-reference has been deprecated at line 228

- update .thtml to .ctp
- included the 3 optional settings for smarty to run:

$this->Smarty->template_dir = VIEWS.DS;
$this->Smarty->cache=true;
$this->Smarty->config_dir = 'config';
3651.0.0.52008-01-29 01:57Toshimasa Matsuoka
Changes since last version::
use for CakePHP 1.2.0.6311(beta)
2801.0.0.42006-11-10 20:13Travis Cline
Changes since last version::
fixing whitespace
2781.0.0.42006-11-05 10:01Travis Cline
Changes since last version::
Allow .thtml for layouts
2521.0.0.32006-10-28 05:30Travis Cline
Changes since last version::
added plugins_dir, register_functions and _getViewFileName override
see http://bakery.cakephp.org/articles/view/124 for usage details (still in moderation at time of publishing)
101.0.0.12006-01-04 10:45Larry E. Masters
Changes since last version::
Passing instance of the SmartyView to the view templates. Needed to call renderElement.

You must use {$view->renderElement()} since smarty is $this instance in the templates
71.0.0.02006-01-01 01:27Larry E. Masters

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0.0.7

<?php
/* SVN FILE: $Id$ */

/**
 * Methods for displaying presentation data
 *
 *
 * PHP versions 4 and 5
 *
 * CakePHP :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright (c) 2005, Cake Software Foundation, Inc.
 *                     1785 E. Sahara Avenue, Suite 490-204
 *                     Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright    Copyright (c) 2005, Cake Software Foundation, Inc.
 * @link         http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
 * @version      1.0.0.7
 * @package      cake
 * @subpackage   cake.app.views
 * @since        CakePHP v 0.10.4.1693
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date$
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 *
 * update: 10/03/07 by tclineks

/**
 * Include Smarty. By default expects it at ( VENDORS.'smarty'.DS.'Smarty.class.php' )
 * vendor('smarty'.DS.'Smarty.class') has been depreciated
 * use App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php'))
 */
App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php'));
/**
 * CakePHP Smarty view class
 *
 * This class will allow using Smarty with CakePHP
 *
 * @version      1.0.0.7
 * @package      cake
 * @subpackage   cake.app.views
 * @since        CakePHP v 0.10.0.1693
 */
class SmartyView extends View
{
/**
 * SmartyView constructor
 *
 * @param  $controller instance of calling controller
 */
	function __construct (&$controller)
	{
		parent::__construct($controller);
		$this->Smarty = &new Smarty();
		// requires views be in a 'smarty' subdirectory, you can remove this limitation if you aren't using other inherited views that use .tpl as the extension
		//$this->subDir = 'smarty'.DS;		
		$this->ext= '.tpl';
		$this->Smarty->plugins_dir[] = VIEWS.'smarty_plugins'.DS;
		$this->Smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS;
		$this->Smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS;
		$this->Smarty->template_dir = VIEWS.DS;
		$this->Smarty->cache=true;
		$this->Smarty->config_dir = 'config';
	}

/**
 * Overrides the View::_render()
 * Sets variables used in CakePHP to Smarty variables
 *
 * @param string $___viewFn
 * @param string $___data_for_view
 * @param string $___play_safe
 * @param string $loadHelpers
 * @return rendered views
 */
	function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true)
	{
		if ($this->helpers != false && $loadHelpers === true)
		{
			$loadedHelpers =  array();
			$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);

			foreach(array_keys($loadedHelpers) as $helper)
			{
				$replace = strtolower(substr($helper, 0, 1));
				$camelBackedHelper = preg_replace('/\\w/', $replace, $helper, 1);

				${$camelBackedHelper} =& $loadedHelpers[$helper];
				if(isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers))
				{
					foreach(${$camelBackedHelper}->helpers as $subHelper)
					{
						${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
					}
				}
				$this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
				$this->Smarty->assign_by_ref($camelBackedHelper, ${$camelBackedHelper});
			}
		}

		$this->register_functions();

		foreach($___data_for_view as $data => $value)
		{
			if(!is_object($data))
			{
				$this->Smarty->assign($data, $value);
			}
		}
		$this->Smarty->assign_by_ref('view', $this);
		return $this->Smarty->fetch($___viewFn);
	}
	
/**
 * Returns layout filename for this template as a string.
 *
 * @return string Filename for layout file (.ctp).
 * @access private
 */
	function _getLayoutFileName() {
		if (isset($this->webservices) && !is_null($this->webservices)) {
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		if (isset($this->plugin) && !is_null($this->plugin)) {
			if (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext)) {
				$layoutFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}
        $paths = App::path('views'); 

        foreach($paths as $path) { 
            if (file_exists($path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext)) { 
                $layoutFileName = $path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext; 
                return $layoutFileName; 
            } 
        } 

        // added for .ctp viewPath fallback 
        foreach($paths as $path) { 
            if (file_exists($path . 'layouts' . DS  . $type . $this->layout . '.ctp')) { 
                $layoutFileName = $path . 'layouts' . DS . $type . $this->layout . '.ctp';
                return $layoutFileName; 
            } 
        }

		if($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $type . $this->layout . '.ctp')) {
		} else {
			$layoutFileName = LAYOUTS . $type . $this->layout.$this->ext;
		}
		return $layoutFileName;
	}

/**
 * Returns filename of given action's template file (.tpl) as a string. CamelCased action names will be under_scored! This means that you can have LongActionNames that refer to long_action_names.ctp views.
 *  - added: will also return .ctp templates in viewPath paths for fallback
 *
 * @param string $action Controller action to find template filename for
 * @return string Template filename
 * @access private
 */
	function _getViewFileName($action) {
		$action = Inflector::underscore($action);
		$paths = App::path('views');

		if (isset($this->webservices) && !is_null($this->webservices)){
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		if (empty($action)) {
			$action = $this->action;
		}

		$position = strpos($action, '..');

		if ($position === false) {
		} else {
			$action = explode('/', $action);
			$i = array_search('..', $action);
			unset($action[$i - 1]);
			unset($action[$i]);
			$action='..' . DS . implode(DS, $action);
		}

		foreach($paths as $path) {
			if (file_exists($path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext)) {
				$viewFileName = $path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
				return $viewFileName;
			}
		}

		// added for .ctp viewPath fallback
		foreach($paths as $path) {
			if (file_exists($path . $this->viewPath . DS . $type . $action . '.ctp')) {
				$viewFileName = $path . $this->viewPath . DS . $type . $action . '.ctp';
				return $viewFileName;
			}
		}

		if ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . $type . $action . '.ctp')) {
		} elseif($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . $this->viewPath . DS . $type . $action . '.ctp')) {
		} else {
			$viewFileName = VIEWS . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
		}

		return $viewFileName;
	}

	/**
	 * checks for existence of special method on loaded helpers, invoking it if it exists
	 * this allows helpers to register smarty functions, modifiers, blocks, etc.
	 */
	function register_functions() {
		foreach(array_keys($this->loaded) as $helper) {
			if (method_exists($this->loaded[$helper], '_register_smarty_functions')) {
				$this->loaded[$helper]->_register_smarty_functions($this->Smarty);
			}
		}
	}
}
?>

		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..