2 mag 2008

CakePHP e i layout (2)



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:

 * 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
 */
?>

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:

 * 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
 */
?>
 
charset(); ?>
 
css('cake.generic'); ?>
<div id="container">
<div id="header">
<h1>CakePHP Rapid Development</h1>
</div>
<div id="content">
			check('Message.flash'))
					{
						$session-&gt;flash();
					}
					echo $content_for_layout;
			?&gt;</div>
<div id="footer">
 
			<a href="http://www.cakephp.org/" target="_new">
				image('cake.power.png', array('alt'=&gt;"CakePHP(tm) : Rapid Development Framework", 'border'=&gt;"0")); ?&gt;
			</a></div>
</div>
 * 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
 */
?&gt;
 
charset(); ?&gt;
 
<!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
-->
 
<a href="&lt;?php echo $url; ?&gt;"></a>

Tags:

Leave a Reply

Message:

  • Ricerca

    or
  • Language

  • Categorie

  • Delicious

  • Statistiche


    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081
    • Pages displayed : 33362
    • Unique visitors : 18465
    • Pages displayed in last 24 hours : 0
    • Unique visitors in last 24 hours : 0