CakePHP - View Template - default values for fetched content?

1.7k views Asked by At

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!

1

There are 1 answers

1
tigrang On BEST ANSWER

Fetch the var. If its not empty, echo it, otherwise echo the default.

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