-- mbneto <[EMAIL PROTECTED]> wrote
(on Monday, 30 July 2007, 03:37 PM -0400):
> I've been using Zend with my projects since 0.7 and using a Smarty class with
> the View to allow me to $this->view->render using the templates.
>
> I've upgraded to Zend 1.0.0 and all my pages broke with a fatal error message
>
> Fatal error: Class Foo_View_Smarty contains 3 abstract methods and must
> therefore be declared abstract or implement the remaining methods
> (Zend_View_Interface::getScriptPaths, Zend_View_Interface::setBasePath,
> Zend_View_Interface::addBasePath)
There was a change to Zend_View_Interface introduced in the 1.0.0RC
series due to, as you mention, the viewRenderer helper.
> The class code is listed bellow but after searching for a while I saw some
> posts regarding the ViewRenderer. Since I'll have to recode some files I am
> looking for a 'definitive' way to integrate Smarty with Zend without having to
> wait until Zend_Layout appears...
There likely will never be a "definitive" way to integrate Smarty, as
it's not part of ZF. ;-)
Another thing to note is that Smarty likes to have all templates grouped
under a common tree. To work around this in projects I've done, I've
made addScriptPath(), setBasePath(), and addBasePath() all work on an
internal path array. Then in render(), I loop over those paths looking
for the template, and, if not found, simply call fetch() on it,
indicating it should fetch from the default template path setup at
Smarty initialization.
So, as an example:
<?php
require_once 'Zend/View/Interface.php';
class Smarty_View implements Zend_View_Interface
{
protected $_scriptPath = array();
protected $_smarty;
public function __construct(array $options = null)
{
if (null !== $options) {
if (!isset($options['smarty'])) {
throw new Exception('Smarty_View requires a Smarty instance');
}
foreach ($options as $key => $value) {
switch ($key) {
case 'smarty':
$this->_smarty = $value;
break;
case 'scriptPath':
$this->setScriptPath($value);
break;
default:
break;
}
}
}
}
public function __set($key, $value)
{
$this->_smarty->assign($key, $value);
}
public function __get($key)
{
return $this->_smarty->get_template_vars($key);
}
public function __isset($key)
{
return (null !== $this->_smarty->get_template_vars($key));
}
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
public function getEngine()
{
return $this->_smarty;
}
public function setScriptPath($path)
{
$path = (array) $path;
foreach ($path as $dir) {
if (!is_dir($dir)) {
throw new Exception('Invalid script path "' . $dir . '"');
}
}
$this->_scriptPath = $path;
return $this;
}
public function setBasePath($path, $prefix = 'Zend_View')
{
$path = ltrim($path, '/');
$path .= '/scripts';
return $this->setScriptPath($path);
}
public function addScriptPath($path)
{
if (!is_string($path) || !is_dir($path)) {
throw new Exception('Invalid script path "' . $path . '"');
}
$this->_scriptPath[] = $path;
return $this;
}
public function addBasePath($path, $prefix = 'Zend_View')
{
$path = rtrim($path, '/');
$path .= '/scripts';
return $this->addScriptPath($path);
}
public function getScriptPath($script)
{
return $this->_getScriptPath($script);
}
public function getScriptPaths()
{
return $this->_scriptPath;
}
public function assign($spec, $value = null)
{
if (is_array($spec)) {
$this->_smarty->assign($spec);
} elseif (is_string($spec) && (null !== $value)) {
$this->_smarty->assign($spec, $value);
} else {
throw new Exception('Invalid call to assign variables; use assoc
array or string/value pair');
}
return $this;
}
public function clearVars()
{
$this->_smarty->clear_all_assign();
return $this;
}
public function render($name)
{
$name = $this->_getScriptPath($name);
return $this->_smarty->fetch($name);
}
protected function _getScriptPath($name)
{
if (!file_exists($name) && (!empty($this->_scriptPath))) {
foreach (array_reverse($this->_scriptPath) as $path) {
$tmp = $path . DIRECTORY_SEPARATOR . $name;
if (file_exists($tmp)) {
$name = $tmp;
break;
}
}
}
return $name;
}
}
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/