Zend Framework: view helper -- Placeholder Helper && RenderToPlcarholder Helper

本文介绍了PlaceholderHelper的功能,包括在viewscript和viewinstances间保存内容、聚合内容、捕获内容及添加前后缀等特性。通过示例展示了如何使用该工具进行内容的存储与捕获,并介绍了一种类似Laravel中section的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值