16

I have a CakePHP application that in some moment will show a view with product media (pictures or videos) I want to know if, there is someway to include another view that threats the video or threats the pictures, depending on a flag. I want to use those "small views" to several other purposes, so It should be "like" a cake component, for reutilization.

What you guys suggest to use to be in Cake conventions (and not using a raw include('') command)

Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51
  • Check out this [link] (http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Views.html) and more specifically the **Elements**-part of that manual page, should tell you everything you need to know – thr Sep 18 '08 at 15:50
  • @FernandoBarrocal: This is a updated link for 2.X version http://book.cakephp.org/2.0/en/views.html#elements – EdgarT Feb 27 '13 at 01:25
  • Does any way to include multiple layout in a main layout, likewise in WordPress there is a function called `get_template_part()` – Sanjay Goswami Nov 02 '15 at 12:20

7 Answers7

9

In the interest of having the information here in case someone stumbles upon this, it is important to note that the solution varies depending on the CakePHP version.

For CakePHP 1.1

$this->renderElement('display', array('flag' => 'value'));

in your view, and then in /app/views/elements/ you can make a file called display.thtml, where $flag will have the value of whatever you pass to it.

For CakePHP 1.2

$this->element('display', array('flag' => 'value'));

in your view, and then in /app/views/elements/ you can make a file called display.ctp, where $flag will have the value of whatever you pass to it.


In both versions the element will have access to all the data the view has access to + any values you pass to it. Furthermore, as someone pointed out, requestAction() is also an option, but it can take a heavy toll in performance if done without using cache, since it has to go through all the steps a normal action would.
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
7

In your controller (in this example the posts controller).

function something() {
    return $this->Post->find('all');
}

In your elements directory (app/views/element) create a file called posts.ctp.

In posts.ctp:

$posts = $this->requestAction('posts/something'); 
foreach($posts as $post): 
    echo $post['Post']['title']; 
endforeach; 

Then in your view:

<?php echo $this->element('posts'); ?>

This is mostly taken from the CakePHP book here: Creating Reusable Elements with requestAction

I do believe that using requestAction is quite expensive, so you will want to look into caching.

Chris Hawes
  • 1,344
  • 1
  • 8
  • 9
7

Simply use:

<?php include('/<other_view>.ctp'); ?>

in the .ctp your action ends up in.

For example, build an archived function

function archived() {
  // do some stuff
  // you can even hook the index() function
  $myscope = array("archived = 1");
  $this->index($myscope);
  // coming back, so the archived view will be launched
  $this->set("is_archived", true); // e.g. use this in your index.ctp for customization
}

Possibly adjust your index action:

function index($scope = array()) {
  // ...
  $this->set(items, $this->paginate($scope));
}

Your archive.ctp will be:

<?php include('/index.ctp'); ?>

Ideal reuse of code of controller actions and views.

LenArt
  • 333
  • 3
  • 6
5

For CakePHP 2.x

New for Cake 2.x is the abilty to extend a given view. So while elements are great for having little bits of reusable code, extending a view allows you to reuse whole views.

See the manual for more/better information

http://book.cakephp.org/2.0/en/views.html#extending-views

Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
1

Elements work if you want them to have access to the same data that the calling view has access to.

If you want your embedded view to have access to its own set of data, you might want to use something like requestAction(). This allows you to embed a full-fledged view that would otherwise be stand-alone.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Lucas Oman
  • 15,597
  • 2
  • 44
  • 45
0

I want to use those "small views" to several other purposes, so It should be "like" a cake component, for reutilization.

This is done with "Helpers", as described here. But I'm not sure that's really what you want. The "Elements" suggestion seems correct too. It heavily depends of what you're trying to accomplish. My two cents...

adriaroca
  • 172
  • 11
Javier Constanzo
  • 443
  • 1
  • 4
  • 16
0

In CakePHP 3.x you can simple use:

$this->render('view')

This will render the view from the same directory as parent view.

vard
  • 4,057
  • 2
  • 26
  • 46
Alex Solovyh
  • 11
  • 1
  • 1