Placeholder Helper
Plcaeholder view helper 用于在view script和view instances之间保存content,而且提供一些有用的特性例如:聚合content、捕获view script content稍后再用、和添加pre- and post-text 到content(以及内容聚合的自定义分隔符)
Example #11 Placeholders的基本用法
基本用法就是储存view DataSet,然后使用设置好的Placehloder name调用Placeholder helper,返回一个对象用来输出。
<?php $this->placeholder('foo')->set("Some text for later") ?>
<?php
echo $this->placeholder('foo');
// outputs "Some text for later"
?>
Example #12 Using Placeholders to Aggregate Content
(这个用法还不明确,以后有用再学习)
Example #13 Using Placeholders to Capture Content
偶尔的,你可能在view script中需要placeholder一些内容来做为template(模板);Placeholder view helper允许你捕获任意content,使用下面的API for later rendering。
captureStart($type, $key) begins capturing content.
$type should be one of the Placeholder constants APPEND or SET. If APPEND, captured content is appended to the list of current content in the placeholder; if SET, captured content is used as the sole value of the placeholder (potentially replacing any previous content). By default, $type is APPEND.
$key can be used to specify a specific key in the placeholder container to which you want content captured.
captureStart() locks capturing until captureEnd() is called; you cannot nest capturing with the same placeholder container. Doing so will raise an exception.
captureEnd() stops capturing content, and places it in the container
object according to how captureStart() was called.
<!-- Default capture: append -->
<?php $this->placeholder('foo')->captureStart();
foreach ($this->data as $datum): ?>
<div class="foo">
<h2><?php echo $datum->title ?></h2>
<p><?php echo $datum->content ?></p>
</div>
<?php endforeach; ?>
<?php $this->placeholder('foo')->captureEnd() ?>
<?php echo $this->placeholder('foo') ?>
<!-- Capture to key -->
<?php $this->placeholder('foo')->captureStart('SET', 'data');
foreach ($this->data as $datum): ?>
<div class="foo">
<h2><?php echo $datum->title ?></h2>
<p><?php echo $datum->content ?></p>
</div>
<?php endforeach; ?>
<?php $this->placeholder('foo')->captureEnd() ?>
<?php echo $this->placeholder('foo')->data ?>
类似于laravel中的section的用法,可以引用“代码块”的方式来实现模板化,减少代码的重复。
RenderToPlaceholder Helper
渲染(renders)一个模板(view script)并存储渲染好的输出作为一个placeholder变量稍后使用。
Example #32 Basic Usage of RenderToPlaceholder
<?php
// View script (backlink.phtml):
echo sprintf(
'<p class="older"><a href="%s">Older posts</a></p>',
$this->url(array('controller' => 'index', 'action' => 'index'))
)
?>
<?php
// Usage:
$this->renderToPlaceholder('backlink.phtml', 'link');
?>
<?php
echo $this->placeholder('link');
// Output: <p class="older"><a href="index/index">Older posts</a></p>
?>