Can anyone explain to me how to use a 'view block' in CakePhP 2.0?
I have read the documentation on the cakephp site but it misses a lot out for the novice user... e.g. what files do I need where, how do you call a block within the code, does the block of code need its own folder/controller/model/view etc? I'm really lost!
If someone could explain it from start to finish on how to use a block as a sidebar that would be great.
The example would be that I have a sidebar that I want to use on different pages but I want to break the sidebar in to different elements to call within the block e.g.
<div class="heading1">
<h2>Heading 1</h2>
</div>
<div class="ul-list1">
<ul>
<li>list item 1</li>
<li>list item 2</li>
</ul>
</div>
<div class="heading2">
<h2>Heading 2</h2>
</div>
<div class="ul-list1">
<ul>
<li>list item 3</li>
<li>list item 4</li>
</ul>
</div>
So break this in to two elements (heading1 and heading 2)
How would I write the code for the block, where where I insert this code and what pages do I need? (please aim this at a novice CakePhP user as I am really confused about this!)
The letter “V” in the MVC is for Views.
Since CakePHP 2.3 this can be simply achieved using CakeResponse. $this->response->file( $data['Asset']['filepath'], array( 'download' => true, 'name' => $data['Asset']['filename'] ) ); We've passed a name parameter to define the filename for the downloaded file.
Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
You should create an element like following.
// app/views/elements/headings.ctp
<?php $this->start('heading1'); ?>
<div class="heading1">
<h2>Heading 1</h2>
</div>
<div class="ul-list1">
<ul>
<li>list item 1</li>
<li>list item 2</li>
</ul>
</div>
<?php $this->end(); ?>
<?php $this->start('heading2'); ?>
<div class="heading2">
<h2>Heading 2</h2>
</div>
<div class="ul-list1">
<ul>
<li>list item 3</li>
<li>list item 4</li>
</ul>
</div>
<?php $this->end(); ?>
// end element file
// include the element first before getting block in the views or layout file.
<?php
echo $this->element('headings');
?>
// after that you will able to display block anywhere inside view files or layout also with following statements.
<?php
// for heading first
echo $this->fetch('heading1');
// for heading second.
echo $this->fetch('heading2');
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With