Cake PhP Blocks - How to use them?
Asked Answered
B

3

10

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!)

Barogram answered 11/10, 2013 at 10:51 Comment(1)
I am still stuck on this - I really need this answering from the point of view of a complete novice! what code to write, where to put that code, in what pages, how to call these? how to use elements etc...?Barogram
H
7

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');
?>
Hubblebubble answered 15/10, 2013 at 5:29 Comment(2)
Hi Robert, if you've still any issue feel free to post comments.Hubblebubble
I still don't get what's the advantage over just using several elements. I'm pretty sure you can do amazing things with blocks, but I fail to think of anything that can't be done with elements more easily and with less lines of code.Acrostic
H
1

you can use the following code in your view or elements.

// for start the block code
$this->start('block_name');

// your html code will goes here, even you can also specify the element reference.

$this->end(); // close the block.

and in your layout, you can fetch/display the view block code as

echo $this->fetch('block_name'); // anywhere in the layout.

make sure that you've specified the same block name in view and layout.

Hubblebubble answered 12/10, 2013 at 1:49 Comment(4)
That's the bit I don't understand. If I add the HTML in to the view I want to see it like the first part says, then whats the point in the block to fetch data? If I use the block code in the elements then where do I put it? could you be really specific on what goes where and in what folders etc please because I'm still confused?Barogram
If you added the block code in element then you can add the element inside the $this->start('block_name'); and $this->end(); in view.Hubblebubble
How do I "even you can also specify the element reference" ?Barogram
I have updated the question to hopefully help me get that extra bit of help because I'm still lost, please help :)Barogram
G
0

In your layout or element when you do :

    $this->fetch ("block_name");
    
No matter when you define your block or you override it ... every time in you others elements you can do $this->start ("block_name") or either $this->append ("block_name") ... cakephp will "merge" all your operation to that block and put them in the appropriate place (defined in your layout)...

An example of using blocks ... imagine one block of css in the header of your layout and javascript block in your footer ... like that if there's specific css or javascript for a specific fonctionnality they will be added to that block and you will not have script and css in the middle of your generated html...

I hope that help you

Garibald answered 17/1, 2017 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.