如何检索布局 zend framework2 中的内容?
How to retrieve content in layout zend framework2?
我在 zend 框架中使用 phtml 文件。现在我正在使用 .tpl 文件。
我找到了如何使用 html 脚本等等。但是当我想使用 php 代码时。然后我使用:
<?php
echo "test";
echo $this->content;
?>
问题在于它在 layout.tpl 文件中。主要内容在其他模块index.tpl
而不是获取索引文件的内容它只回显 'test'。如何让它工作?
已编辑:我也试过 {$this->content}
。
如果您正在使用 Smarty 模板引擎和 SmartyModule, then you will have to use Smarty syntax in your view scripts, since the Zend\View\Renderer\PhpRenderer
will be overridden by the Smarty Renderer (and the Smarty Templating Engine). Also, if you wish to use layouts with Smarty, please see Smarty's Template Inheritance 机制。这是一个例子:
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
否则,如果您使用的是 PhpRenderer,它不会 "recognize" 任何模板语言,即使您将视图脚本的文件扩展名更改为 .tpl,因为它只会 include
内容视图脚本(参见渲染器 source code 的第 502-503 行)。因此,与任何 include
一样,PHP 代码将立即执行并存储在渲染器的 $__content
属性 中。这可能是您的 echo
命令被立即执行的原因。
所以,基本上,您必须选择渲染器(通过 SmartyModule 的 PhpRenderer 或 Smarty 渲染器),然后遵守其内部工作原理(PHP/HTML 或 Smarty 语法(例如 variables) , 分别).
我在 zend 框架中使用 phtml 文件。现在我正在使用 .tpl 文件。 我找到了如何使用 html 脚本等等。但是当我想使用 php 代码时。然后我使用:
<?php
echo "test";
echo $this->content;
?>
问题在于它在 layout.tpl 文件中。主要内容在其他模块index.tpl
而不是获取索引文件的内容它只回显 'test'。如何让它工作?
已编辑:我也试过 {$this->content}
。
如果您正在使用 Smarty 模板引擎和 SmartyModule, then you will have to use Smarty syntax in your view scripts, since the Zend\View\Renderer\PhpRenderer
will be overridden by the Smarty Renderer (and the Smarty Templating Engine). Also, if you wish to use layouts with Smarty, please see Smarty's Template Inheritance 机制。这是一个例子:
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
否则,如果您使用的是 PhpRenderer,它不会 "recognize" 任何模板语言,即使您将视图脚本的文件扩展名更改为 .tpl,因为它只会 include
内容视图脚本(参见渲染器 source code 的第 502-503 行)。因此,与任何 include
一样,PHP 代码将立即执行并存储在渲染器的 $__content
属性 中。这可能是您的 echo
命令被立即执行的原因。
所以,基本上,您必须选择渲染器(通过 SmartyModule 的 PhpRenderer 或 Smarty 渲染器),然后遵守其内部工作原理(PHP/HTML 或 Smarty 语法(例如 variables) , 分别).