cakephpdefault-valueview-templates

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!


Solution

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

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