2
我想用佈局渲染我的把手模板。我試圖從Zend_View_Abstract類中編寫我自己的View,基於我爲Smarty/ZF1找到的一些教程而鬆散地進行。不知道我是否以正確的方式開展工作,必須採用更簡單的方式(控制器方法,幫手等)。反正我會後我有什麼:如何在Zend框架1中使用模板引擎(小鬍子,把手)?
<?php
use Handlebars\Handlebars;
class Handlebars_View extends Zend_View_Abstract
{
private $engine = false;
public function __construct($data = array())
{
parent::__construct($data);
$this->engine = new Handlebars;
}
public function assign($spec, $value=null)
{
}
public function escape($var)
{
if (is_string($var))
{
return parent::escape($var);
}
elseif (is_array($var))
{
foreach ($var as $key => $val)
{
$var[$key] = $this->escape($val);
}
return $var;
}
else
{
return $var;
}
}
public function render($name)
{
// this will get me /var/www/shared-views-zf/application/views/scripts/
// I don't know how to get the rest of the path e.g. 'users/login.phtml'
//$viewScriptsPath = $this->getAllPaths()['scripts'][1]; // incomplete
//$viewScriptsPath.= $viewNameFromSomewhere; // ??
//$template = file_get_contents($viewScriptsPath); can't do this yet
// get data which was pre assigned
//$data =
//
//$content = $this->engine->render($template, $data)
// i can set the layout content here (but how do I get the content)
$this->layout()->content = 'Heloo';
// getlayout full path
require $this->getScriptPath($name); // require /var/www/shared-views-zf/application/layouts/scripts/layout.phtml
}
public function _run() {
}
}
顯然我的渲染功能是缺失的部分,但希望可以看到我試圖去的方向。另外,我在我的引導下:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initViews()
{
$view = new Handlebars_View();
$viewRender = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRender->setView($view);
$viewRender->setViewSuffix('phtml');
Zend_Controller_Action_HelperBroker::addHelper($viewRender);
return $view;
}
}
OK,我知道我可能要對這個完全錯誤的方式。在它最簡單的形式,youu實現把手以這樣的方式:
use Handlebars\Handlebars;
$engine = new Handlebars;
$template = file_get_contents($filePath);
$result = $engine->render(
$template,
$data
);
..但可以看出我需要有$文件路徑(查看腳本文件),$數據(數組被傳遞到訪問鑑於從控制器),然後我可以從View類的佈局設置如下:
$this->layout()->content = $result;
require $layoutPath;
我會很感激,如果有人可以解釋應該是這樣的方式。我真的沒有在網上閱讀文檔或閱讀教程。此外,我有點困擾ZF1,因爲它是我們大部分應用程序的寫入內容。感謝您的幫助。