PHP - ZF2 - 从字符串变量渲染模板

时间:2017-03-31 13:48:10

标签: php html zend-framework2 render

我在ZF2中渲染模板时遇到问题,其中模板在变量中的字符串中。有一个简单的例子:

$template = "<div>easy</div>";
$view = new \Zend\View\Model\ViewModel();
$view->setTemplate($template);   

$renderer = new \Zend\View\Renderer\PhpRenderer();
$html = $renderer->render($view);

此代码在渲染时失败,渲染器认为模板是文件的路径。而iam reallz不知道如何告诉rendere它是一个字符串。

感谢您的时间并做出回应。

2 个答案:

答案 0 :(得分:0)

尝试替换&#39; \&#39; _下划线为Zend_View_Renderer_PhpRenderer

答案 1 :(得分:0)

您必须扩展PhpRenderer类并覆盖render方法,以便将$ template中的字符串用作实际模板:

class MyPhpRenderer extends PhpRenderer {
public function render($nameOrModel, $values = null)
{
    if ($nameOrModel instanceof Model) {
        $model       = $nameOrModel;
        $nameOrModel = $model->getTemplate();
        if (empty($nameOrModel)) {
            throw new Exception\DomainException(sprintf(
                '%s: received View Model argument, but template is empty',
                __METHOD__
            ));
        }
        $options = $model->getOptions();
        foreach ($options as $setting => $value) {
            $method = 'set' . $setting;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
            unset($method, $setting, $value);
        }
        unset($options);

        // Give view model awareness via ViewModel helper
        $helper = $this->plugin('view_model');
        $helper->setCurrent($model);

        $values = $model->getVariables();
        unset($model);
    }

    // find the script file name using the parent private method
    $this->addTemplate($nameOrModel);
    unset($nameOrModel); // remove $name from local scope

    $this->__varsCache[] = $this->vars();

    if (null !== $values) {
        $this->setVars($values);
    }
    unset($values);

    // extract all assigned vars (pre-escaped), but not 'this'.
    // assigns to a double-underscored variable, to prevent naming collisions
    $__vars = $this->vars()->getArrayCopy();
    if (array_key_exists('this', $__vars)) {
        unset($__vars['this']);
    }
    extract($__vars);
    unset($__vars); // remove $__vars from local scope

    while ($this->__template = array_pop($this->__templates)) {
        $this->__file = $this->resolver($this->__template);
        try {
            if (!$this->__file) {
               $this->__content = $this->__template; // this line does what you need
            }else{
              ob_start();
              $includeReturn = include $this->__file;
              $this->__content = ob_get_clean();
            }
        } catch (\Exception $ex) {
            ob_end_clean();
            throw $ex;
        }
        if ($includeReturn === false && empty($this->__content)) {
            throw new Exception\UnexpectedValueException(sprintf(
                '%s: Unable to render template "%s"; file include failed',
                __METHOD__,
                $this->__file
            ));
        }
    }

    $this->setVars(array_pop($this->__varsCache));

    if ($this->__filterChain instanceof FilterChain) {
        return $this->__filterChain->filter($this->__content); // filter output
    }

    return $this->__content;
}
}

然后你code应该是这样的:

$template = "<div>easy</div>";
$view = new \Zend\View\Model\ViewModel();
$view->setTemplate($template);   

$renderer = new MyPhpRenderer();
$html = $renderer->render($view);