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