Skip to content

Instantly share code, notes, and snippets.

@shawndreck
Created October 17, 2012 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawndreck/3904016 to your computer and use it in GitHub Desktop.
Save shawndreck/3904016 to your computer and use it in GitHub Desktop.
integrating smarty 3 with zend application
<?php
/**
* Uses smarty template engine to render html pages
* @author shawndreck
*
*/
require_once APPLICATION_PATH . '/../lib/Smarty/SmartyBC.class.php';
class ZendExt_View_Smarty3 implements Zend_View_Interface
{
/**
* Smarty object
* @var Smarty
*/
protected $_smarty;
/**
* Default file that will be used as the global
*/
protected $default_template;
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array()) {
$this->_smarty = new Smarty;
if (null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}
foreach ($extraParams as $key => $value) {
if(property_exists($this->_smarty, $key)){
$this->_smarty->$key = $value;
}else if(property_exists($this, $key)){
$this->$key = $value;
}
}
$this->assign('this', $this);
}
/**
* Return the template engine object
*
* @return Smarty
*/
public function getEngine()
{
return $this->_smarty;
}
/**
* Set the path to the templates
*
* @param string $path The directory to set as the path.
* @return void
*/
public function setScriptPath($path)
{
// p($path,1);
// p(debug_print_backtrace(),1);
if (is_readable($path)) {
$this->_smarty->template_dir = $path;
return;
}
throw new Exception('Invalid path provided');
}
/**
* Retrieve the current template directory
*
* @return string
*/
public function getScriptPaths()
{
if(is_array($this->_smarty->template_dir)){
return $this->_smarty->template_dir;
} else {
return array($this->_smarty->template_dir);
}
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function setBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function addBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val)
{
$this->assign($key, $val);
}
/**
* Magic getter for Zend_View compatibility. Retrieves template var
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if(!$val = $this->_smarty->getTemplateVars($key)){
return null;
}
return $val;
}
public function escape(){
if (is_string($var)) {
return parent::escape($var);
} elseif (is_array($var)) {
foreach ($var as $key => $val) {
$var[$key] = $this->escape($val);
}
}
return $var;
}
public function setTemplateCompilePath($path)
{
$this->_smarty->compile_dir = $path;
}
public function setTemplateCompileID($id)
{
$this->_smarty->compile_id = $id;
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (null !== $this->_smarty->getTemplateVars($key));
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clearAssign($key);
}
/**
* Zend_View compatibility. Retrieves all template vars
*
* @see Zend_View_Abstract::getVars()
* @return array
*/
public function getVars()
{
return $this->_smarty->getTemplateVars();
}
public function getTemplateVars($var = NULL) {
return $this->_smarty->getTemplateVars($var);
}
/**
* Overwrite assign() method
*
* The next part is an overwrite of the assign() method
* from Zend_View_Abstract, which works in a similar way.
* The big difference is that the values are assigned to
* the Smarty object and not to the $this->_vars
* variables array of Zend_View_Abstract.
*
* @param string|array $var
* @return ZendExt_View_Smarty
*/
public function assign($var, $value = null)
{
if (is_string($var)) {
$this->_smarty->assign($var, $value);
} elseif (is_array($var)) {
foreach ($var as $key => $value) {
$this->assign($key, $value);
}
} else {
throw new Zend_View_Exception('assign() expects a string or array, got '.gettype($var));
}
return $this;
}
public function append($spec, $value = NULL) {
if (is_array($spec)) {
$this->_smarty->append($spec);
return;
}
$this->_smarty->append($spec, $value);
}
/**
* Zend_View compatibility. Removes all template vars
*
* @see View/Zend_View_Abstract::clearVars()
* @return ZendExt_View_Smarty
*/
public function clearVars()
{
$this->_smarty->clearAllAssign();
$this->assign('this', $this);
return $this;
}
/**
* Use Smarty caching
*
* The last two methods were created to simply integrate the Smarty caching mechanism in the View class. With the first one you can check for cached template and with the second one you can set the caching on or of.
*
* @param string $template
* @return bool
*/
public function isCached($template)
{
return $this->_smarty->isCached($template);
}
/**
* Enable/disable caching
*
* @param bool $caching
* @return ZendExt_View_Smarty
*/
public function setCaching($caching)
{
$this->_smarty->caching = $caching;
return $this;
}
/**
* Print the output
*
* The next method output() is a wrapper on the render() method from Zend_View_Abstract. It just sets some headers before printing the output.
*
* @param <type> $name
*/
public function output($name)
{
$this->render($name);
}
/**
* Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function render($name) {
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Cache-Control: post-check=0, pre-check=0", false);
// check if the request is ajax and strip the scrape out the head
// p($this->getScriptPaths(),1);
$data = '';
if($this->default_template){
// make sure that the default template exists
$paths = (array)$this->_smarty->getTemplateDir();
foreach($paths as $path){
if(is_readable($path . '/' . $this->default_template)){
// This default_template is designed to call the right inner templates
$data = $this->_smarty->fetch($this->default_template);
break;
}
if(!$data && is_readable($path . '/scripts/' . $name)){
$data = $this->_smarty->fetch('scripts/' . $name);
v($data,1);
break;
}
}
}else{
$data = $this->_smarty->fetch($name);//fetch($name);//parent::render($name);
}
echo $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment