简体   繁体   English

CakePHP-查看模板-获取内容的默认值?

[英]CakePHP - View Template - default values for fetched content?

This is the example in the book, of a simple View Template 这是本书中的一个简单视图模板示例

// app/View/Common/view.ctp 
<h1><?php echo $this->fetch('title'); ?></h1> 
<?php echo $this->fetch('content'); ?>

<div class="actions">
    <h3>Related actions</h3>
    <ul>
    <?php echo $this->fetch('sidebar'); ?>
    </ul> </div>

And this is how it might be extended 这就是它可能会扩展的方式

// app/View/Posts/view.ctp
<?php
$this->extend('/Common/view');

$this->assign('title', $post);

My question is: How can I set a default value/content for (let's say) the title , in order to overwrite if needed ? 我的问题是: 如何设置标题的默认值/内容(比如说),以便在需要时覆盖?

Thank you! 谢谢!

Fetch the var. 获取变量。 If its not empty, echo it, otherwise echo the default. 如果它不为空,则回显它,否则回显默认值。

$title = $this->fetch('title');
echo !empty($title) ? $title : 'Default';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM