<?xml version="1.0" encoding="UTF-8"?>
<post>
  <alt nil="true"></alt>
  <body>&lt;p&gt;Comme Rails est un Framework(on ne va pas radoter), il a son propre syst&#232;me de Layout assez avanc&#233;.&lt;br/&gt;
Pour ceux qui ont d&#233;j&#224; fait leur premiers pas sous RoR, ils peuvent sauter
l'introduction...&lt;/p&gt;

&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;Voici une arborescence d'une application classique sous Rails avec un simple Controleur (User dans notre cas) ayant 5 actions (g&#233;n&#233;r&#233;es par SCAFFOLD : edit, index, new, show et destroy (qui ne contient de pas de vue).&lt;/p&gt;
&lt;img src=&quot;/images/application-scaffold-rails.png&quot; alt=&quot;Arborescence Application Ruby on Rails G&#233;n&#233;r&#233; par Scaffold&quot;/&gt;
&lt;p&gt;Par d&#233;faut, les vues des actions (ou m&#233;thodes) du controleur User utilise le Layout &lt;span style=&quot;font-family: Courier New,Courier,monospace;&quot;&gt;users.html.erb&lt;/span&gt;. En absence de ce dernier il utilisera le Layout &lt;span style=&quot;font-family: Courier New, Courier,monospace;&quot;&gt;application.html.erb&lt;/span&gt;. D'ailleurs, la plupart de petites applications sous Rails, mettent l'en-t&#234;te (header) et le pied de page (footer) dans ce fichier, et ce
qui change dans le body seront les vues des actions.
&lt;/p&gt;
&lt;h3&gt;Selection du Layout &#224; partir du Controleur&lt;/h3&gt;
&lt;p&gt;Il est possible de selectionner un layout au niveau du controleur en fonction de la m&#233;thode autre que celui de la vue par d&#233;faut ou bien &lt;span style=&quot;font-family: Courier New,Courier,monospace;&quot;&gt;application.html.erb&lt;/span&gt;&lt;br/&gt; ci dessous un exemple parlant.
&lt;/p&gt;

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;UserController&lt;/span&gt; &amp;gt; &lt;span class=&quot;co&quot;&gt;ApplicationController&lt;/span&gt;
  layout &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;patron&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:except&lt;/span&gt; =&amp;gt; [ &lt;span class=&quot;sy&quot;&gt;:index&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:edit&lt;/span&gt; ]
  ....

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;ou bien ...&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;UserController&lt;/span&gt; &amp;gt; &lt;span class=&quot;co&quot;&gt;ApplicationController&lt;/span&gt;
  layout &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;patron&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:only&lt;/span&gt; =&amp;gt; [ &lt;span class=&quot;sy&quot;&gt;:new&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:show&lt;/span&gt; ]
  ....

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;on peut encore aller plus loins&lt;/p&gt;

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;UserController&lt;/span&gt; &amp;gt; &lt;span class=&quot;co&quot;&gt;ApplicationController&lt;/span&gt;

layout &lt;span class=&quot;sy&quot;&gt;:layout_utilise&lt;/span&gt;
...

private

&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;layout_utilise&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;User&lt;/span&gt;.is_logged?
     &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;admin&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Pas mal de fois, quand on utilise des appels AJAX pour changer le contenu d'un div ou autre, on n'a pas besoin d'avoir le layout qui encapsule la vue de l'action de cet appel.&lt;br/&gt;
Ainsi on utilise le code suivant&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;appelajax&lt;/span&gt;
  ...
  render(&lt;span class=&quot;sy&quot;&gt;:layout&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;false&lt;/span&gt;)
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Il reste encore d'autres fa&#231;ons de sp&#233;cifier le layout par rapport &#224; une vue, &#231;a sera dans un futur article&lt;/p&gt;</body>
  <cat-id type="integer">1</cat-id>
  <created-at type="datetime">2009-06-02T12:56:41Z</created-at>
  <id type="integer">4</id>
  <pagetitle>Gestion des Layout sous Ruby on Rails</pagetitle>
  <photo-content-type nil="true"></photo-content-type>
  <photo-file-name nil="true"></photo-file-name>
  <photo-file-size type="integer" nil="true"></photo-file-size>
  <short>Quelques recette pour l'utilisation du Layout (patron) sous Rails</short>
  <title>Layout et vues</title>
  <updated-at type="datetime">2009-06-02T13:21:05Z</updated-at>
  <url>layout-rails</url>
</post>
