Cakephp Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Views

Using view blocks

View blocks replace $scripts_for_layout and provide a flexible API that allows you to define slots or
blocks in your views/layouts that will be defined elsewhere. For example, blocks are ideal for implementing
things such as sidebars, or regions to load assets at the bottom/top of the layout. Blocks can be defined in
two ways: either as a capturing block, or by direct assignment. The start(), append() and end()
methods allow you to work with capturing blocks:

// Create the sidebar block.
$this->start('sidebar');
echo $this->element('sidebar/recent_topics');
echo $this->element('sidebar/recent_comments');
$this->end();
// Append into the sidebar later on.
$this->append('sidebar');
echo $this->element('sidebar/popular_topics');
$this->end();

You can also append into a block using start() multiple times. assign() can be used to clear or
overwrite a block at any time:

// Clear the previous content from the sidebar block.
$this->assign('sidebar', '');

In 2.3, a few new methods were added for working with blocks. The prepend() method was added to
prepend content to an existing block:

// Prepend to sidebar
$this->prepend('sidebar', 'this content goes on top of sidebar');

The method startIfEmpty() can be used to start a block only if it is empty or undefined. If the block
already exists, the captured content will be discarded. This is useful when you want to conditionally define
default content for a block if it does not already exist:

// In a view file.
// Create a navbar block
$this->startIfEmpty('navbar');
echo $this->element('navbar');
echo $this->element('notifications');
$this->end();

// In a parent view/layout
<?php $this->startIfEmpty('navbar'); ?>
<p>If the block is not defined by now - show this instead</p>
<?php $this->end(); ?>
// Somewhere later in the parent view/layout
echo $this->fetch('navbar');

In the above example, the navbar block will only contain the content added in the first section. Since the
block was defined in the child view, the default content with the <p> tag will be discarded.

Displaying blocks

You can display blocks using the fetch() method. fetch() will safely output a block, returning ‘’ if a
block does not exist:

echo $this->fetch('sidebar');

You can also use fetch to conditionally show content that should surround a block should it exist. This is
helpful in layouts, or extended views where you want to conditionally show headings or other markup:

// In app/View/Layouts/default.ctp
<?php if ($this->fetch('menu')): ?>
<div class="menu">
<h3>Menu options</h3>
<?php echo $this->fetch('menu'); ?>
</div>
<?php endif; ?>

As of 2.3.0, you can also provide a default value for a block should it not have any content. This allows
you to easily add placeholder content for empty states. You can provide a default value using the second
argument:

<div class="shopping-cart">
<h3>Your Cart</h3>
<?php echo $this->fetch('cart', 'Your cart is empty'); ?>
</div>

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)