如何在Zend框架1中使用模板引擎(小胡子,把手)?

时间:2014-11-06 15:22:01

标签: php zend-framework handlebars.js

我想用布局渲染我的手柄模板。我试图根据我为Smarty / ZF1遇到的一些教程,松散地从Zend_View_Abstract类编写我自己的View。不知道我是否以正确的方式去做,必须是一种更简单的方法(控制器方法,助手等)。无论如何,我会发布我所拥有的内容:

<?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;
    }
}

好的,我知道我可能会以完全错误的方式解决这个问题。在它最简单的形式中,你将以这样的方式实现把手:

use Handlebars\Handlebars;

$engine = new Handlebars;

$template = file_get_contents($filePath);

$result = $engine->render(
    $template,
    $data
);

..但是可以看出我需要访问$ filePath(查看脚本文件),$ data(从控制器传递给视图的数组),然后我可以从我的View类设置布局为这样:

$this->layout()->content = $result;
require $layoutPath;

如果有人能解释应该这样做的话,我真的很感激。我没有真正随时随地使用文档或在线阅读教程。另外,由于我们的大多数应用程序都是用来编写的,因此我很痴迷于ZF1。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

试试这个(未经过测试,但会让你到达或非常接近)。

在渲染功能中:

public function render($name)
{ 
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    // optional if you want to fallback to phtml
    if ($ext === 'hbs') {
        // get data which was assigned at controller level
        $data = $this->getVars();
        // it may get mad at this part!
        $template = file_get_contents($this->getScriptPath($name));
        // render
        $res = $this->engine->render($template, $data);
    } else {
        $res = parent::render($name);
    }
    return $res;
}