Che cosa sono i layout in CakePHP ed a cosa servono?
Essenzialmente i layout sono dei file contenenti codice di presentazione che definisce come mostrare i dati delle views. Ogni view quindi dovrebbe essere visualizzata all’interno di un layout.
E’ importante ora notare che CakePHP è disponibile attualmente in due versioni:
- Stable: 1.1.19.6305
- Beta: 1.2.0.6311
Queste due versioni si differenziano tra le altre cose per il fatto che la prima versione contiene solo i layout default, ajax e flash mentre la versione 1.2 Beta contiene i layout default, ajax, flash, xml, js, e rss.
Questi layout sono già all’interno di CakePHP e si trovano nella cartella /cake/libs/view/templates/layouts.
Quanto segue fa sempre riferimento alla versione Stable 1.1.19.6305 di CakePHP (la versione 1.2 Beta usa l’estensione ctp per i layout invece dell’estensione thtml usata nella 1.1.19.6305).
All’interno della classe Controller alla riga 142 viene specificato come layout quello di default, che si trova nella cartella /app/views/layouts/default.thtml. Pertanto se vogliamo usare un layout creato da noi per tutte le pagine basterà modificare il file default.thtml. Se volessimo ripristinare un giorno il layout di default di CakePHP basterà ricopiarlo dalla cartella /cake/libs/view/templates/layouts.
Quando si crea un nuovo layout è necessario indicare a CakePHP dove inserire il codice generato dalle views. Per fare questo si inserisce questa riga:
$content_for_layout //and optionally $title_for_layout
Per esempio andiamo a vedere il layout ajax, già presente in entrambe le versioni di CakePHP:
<?php /* SVN FILE: $Id: ajax.thtml 6305 2008-01-02 02:33:56Z phpnut $ */ /** * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.view.templates.layouts * @since CakePHP(tm) v 0.10.0.1076 * @version $Revision: 6305 $ * @modifiedby $LastChangedBy: phpnut $ * @lastmodified $Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ ?> <?php echo $content_for_layout; ?>
Come si può notare il layout ajax non fa altro che stampare esattamente i dati generati dalla view, risulta pertanto utile nel caso analizzato in precedenza.
Non c’è limite al infine al numero di layout che si possono creare, basta salvarli nella cartella /app/views/layouts (dove app è il nome dell’applicazione di CakePHP) e specificare all’interno del controller di usare il layout desiderato, con la seguente funzione (immaginando di aver creato un file my_layout.thtml ed averlo salvato nella cartella appena citata):
// inside your controller file $this->layout = 'my_layout'; // you can use setLayout() too
Per completezza riporto qui sotto il codice degli altri due layout predefiniti di CakePHP, ovvero default e flash:
<?php /* SVN FILE: $Id: default.thtml 6305 2008-01-02 02:33:56Z phpnut $ */ /** * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.view.templates.pages * @since CakePHP(tm) v 0.10.0.1076 * @version $Revision: 6305 $ * @modifiedby $LastChangedBy: phpnut $ * @lastmodified $Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CakePHP(tm) : <?php echo $title_for_layout; ?></title> <?php echo $html->charset(); ?> <link rel="icon" href="<?php echo $this->webroot . 'favicon.ico'; ?>" type="image/x-icon" /> <link rel="shortcut icon" href="<?php echo $this->webroot . 'favicon.ico'; ?>" type="image/x-icon" /> <?php echo $html->css('cake.generic'); ?> </head> <body> <div id="container"> <div id="header"> <h1>CakePHP Rapid Development</h1> </div> <div id="content"> <?php if ($session->check('Message.flash')) { $session->flash(); } echo $content_for_layout; ?> </div> <div id="footer"> <a href="http://www.cakephp.org/" target="_new"> <?php echo $html->image('cake.power.png', array('alt'=>"CakePHP(tm) : Rapid Development Framework", 'border'=>"0")); ?> </a> </div> </div> <?php echo $cakeDebug; ?> </body> </html>
<?php /* SVN FILE: $Id: flash.thtml 6305 2008-01-02 02:33:56Z phpnut $ */ /** * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.view.templates.layouts * @since CakePHP(tm) v 0.10.0.1076 * @version $Revision: 6305 $ * @modifiedby $LastChangedBy: phpnut $ * @lastmodified $Date: 2008-01-01 20:33:56 -0600 (Tue, 01 Jan 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><?php echo $page_title; ?></title> <?php echo $html->charset(); ?> <?php if (Configure::read() == 0) { ?> <meta http-equiv="Refresh" content="<?php echo $pause; ?>;url=<?php echo $url; ?>"/> <?php } ?> <style><!-- P { text-align:center; font:bold 1.1em sans-serif } A { color:#444; text-decoration:none } A:HOVER { text-decoration: underline; color:#44E } --></style> </head> <body> <p><a href="<?php echo $url; ?>"><?php echo $message; ?></a></p> </body> </html>

