Integrating Zend and Smarty
Two easy step to integrating Zend Framework and Smarty Template Engine.
Simple Zend_View_Interface Implementation
<?php
require_once 'Zend/View/Interface.php';
require_once 'Smarty/Smarty.class.php';
/**
* Simple implementation for Zend View Interface using Smarty Template Engine
*
* @author fridayana baabullah
*/
class App_View_Smarty implements Zend_View_Interface{
/**
* @var Smarty
*/
private $smarty;
public function __construct($base) {
/* Set up Smarty as usual */
$this->smarty = new Smarty();
$this->smarty->template_dir = $base . 'templates';
$this->smarty->compile_dir = $base . 'templates_c';
$this->smarty->config_dir = $base . 'configs';
$this->smarty->cache_dir = $base . 'cache';
}
/**
* Return the template engine object, if any
*
* If using a third-party template engine, such as Smarty, patTemplate,
* phplib, etc, return the template engine object. Useful for calling
* methods on these objects, such as for setting filters, modifiers, etc.
*
* @return Smarty
*/
public function getEngine() {
return $this->smarty;
}
/**
* Set the path to find the view script used by render()
*
* @param string|array The directory (-ies) to set as the path. Note that
* the concrete view implentation may not necessarily support multiple
* directories.
* @return void
*/
public function setScriptPath($path) {
if (is_readable($path)) {
$this->smarty->template_dir = $path;
}
}
/**
* Retrieve all view script paths
*
* @return array
*/
public function getScriptPaths() {
return array($this->smarty->template_dir);
}
/**
* Set a base path to all view resources
*
* @param string $path
* @param string $classPrefix
* @return void
*/
public function setBasePath($path, $classPrefix = 'Zend_View') {
return $this->setScriptPath($path);
}
/**
* Add an additional path to view resources
*
* @param string $path
* @param string $classPrefix
* @return void
*/
public function addBasePath($path, $classPrefix = 'Zend_View') {
// Ignored! Cannot add additional path to smarty
}
/**
* Assign a variable to the view
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val) {
$this->smarty->assign($key, $val);
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key) {
return $this->smarty->get_config_vars($key) !== null;
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key) {
$this->smarty->clear_assign($key);
}
/**
* Assign variables to the view script via differing strategies.
*
* Suggested implementation is to allow setting a specific key to the
* specified value, OR passing an array of key => value pairs to set en
* masse.
*
* @see __set()
* @param string|array $spec The assignment strategy to use (key or array of key
* => value pairs)
* @param mixed $value (Optional) If assigning a named variable, use this
* as the value.
* @return void
*/
public function assign($spec, $value = null) {
if (is_array($spec)) {
$this->smarty->assign($spec);
return;
}
$this->smarty->assign($spec, $value);
}
/**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via {@link assign()} or
* property overloading ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars() {
return $this->smarty->clear_all_assign();
}
/**
* Processes a view script and returns the output.
*
* @param string $name The script script name to process.
* @return string The script output.
*/
public function render($name) {
return $this->smarty->fetch($name);
}
}
Configure ViewRenderer Helper
require_once 'App/View/Smarty.php';
require_once 'Zend/Controller/Action/HelperBroker.php';
require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
$view = new App_View_Smarty('/opt/project/smarty/');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
Your controller code is remain the same:
require_once 'App/Controller/Base.php';
/**
* Default action controller
*
*/
class IndexController extends App_Controller_Base {
public function indexAction() {
$this->view->assign('first_name', 'fridayana');
$this->view->last_name = 'baabullah';
}
}
Here is the smarty template file (index.tpl):
Hello {$first_name} {$last_name}


0 Komentar:
Poskan Komentar
Berlangganan Poskan Komentar [Atom]
<< Beranda