{ID} - 3.5.2.3 Page-related Attributes: $layout and $pageTitle

Ada beberapa atribut di controller CakePHP yang memberikan anda kontrol bagaimana views anda diset didalam sebuah layout.

Atribut $layout dapat diset dengan nama dari layout yang disimpan di dalam /app/views/layouts. Anda dapat menentukan sebuah layout dengan menset $layout sesuai dengan nama berkas (file) layout, tanpa ekstensi .ctp. Jika atribut ini tidak didefinisikan, maka CakePHP akan merender layout default, default.ctp. Jika anda belum membuat file layout default di /app/views/layouts/default.ctp, maka layout default dari inti CakePHP-lah yang akan dirender.

<?php

//   Menggunakan $layout untuk mendefinisikan layout alternatif

class RecipesController extends AppController {
    function quickSave() {
        $this->layout = 'ajax';
    }
}

?>
  1. <?php
  2. // Menggunakan $layout untuk mendefinisikan layout alternatif
  3. class RecipesController extends AppController {
  4. function quickSave() {
  5. $this->layout = 'ajax';
  6. }
  7. }
  8. ?>

Anda juga bisa merubah title (judul) dari sebuah halaman (terletak di atas bar dari perambah (browser) anda) dengan menggunakan $pageTitle. Agar hal ini dapat bekerja dengan benar, layout anda harus memasukkan variabel $title_for_layout, yang diletakkan di antara tag <title> dan </title> pada bagian awal dokumen HTML.

<?php

//   Menggunakan $pageTitle untuk mendefinisikan judul halaman

class RecipesController extends AppController {
    function quickSave() {
        $this->pageTitle = 'My search engine optimized title';
    }
}

?>
  1. <?php
  2. // Menggunakan $pageTitle untuk mendefinisikan judul halaman
  3. class RecipesController extends AppController {
  4. function quickSave() {
  5. $this->pageTitle = 'My search engine optimized title';
  6. }
  7. }
  8. ?>

Anda juga bisa menset judul halaman dari dalam controller menggunakan $this->pageTitle (Anda harus memasukkan bagian $this->). Hal ini direkomendasikan, karena akan lebih baik untuk memisahkan bagian logika dari layout dan isi (content). Untuk halaman statis, anda harus mendefinisikan $this->pageTitle di dalam view untuk mendapatkan judul kustom (yang sesuai).

Jika $this->pageTitle tidak diset, maka judul halaman otomatis diganti dengan judul berdasarkan nama controller. Jika merupakan halaman statis, maka nama dari berkas (file) view lah yang akan digunakan sebagai judul.

{EN} - 3.5.2.3 Page-related Attributes: $layout and $pageTitle

A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout.

The $layout attribute can be set to the name of a layout saved in /app/views/layouts. You specify a layout by setting $layout equal to the name of the layout file minus the .ctp extension. If this attribute has not been defined, CakePHP renders the default layout, default.ctp. If you haven’t defined one at /app/views/layouts/default.ctp, CakePHP’s core default layout will be rendered.

<?php

//   Using $layout to define an alternate layout

class RecipesController extends AppController {
    function quickSave() {
        $this->layout = 'ajax';
    }
}

?>
  1. <?php
  2. // Using $layout to define an alternate layout
  3. class RecipesController extends AppController {
  4. function quickSave() {
  5. $this->layout = 'ajax';
  6. }
  7. }
  8. ?>

You can also change the title of the page (that is located in the bar at the top of your browser) using $pageTitle. In order for this to work properly, your layout needs to include the $title_for_layout variable, at least between the <title> and </title> tags in the head of the HTML document.

<?php

//   Using $pageTitle to define the page title

class RecipesController extends AppController {
    function quickSave() {
        $this->pageTitle = 'My search engine optimized title';
    }
}

?>
  1. <?php
  2. // Using $pageTitle to define the page title
  3. class RecipesController extends AppController {
  4. function quickSave() {
  5. $this->pageTitle = 'My search engine optimized title';
  6. }
  7. }
  8. ?>

You can also set the page title from the view using $this->pageTitle (You must include the $this-> part.) This is recommended, as it better separates the logic from the layout and content. For a static page you must use $this->pageTitle in the view if you want a custom title.

If $this->pageTitle is not set, a title will be automatically generated based on the controller name, or the view file name in the case of a static page.

Differences

Lines: 1-15Lines: 1-14
 <title>Page-related Attributes: $layout and $pageTitle</title> <title>Page-related Attributes: $layout and $pageTitle</title>
-<p>
A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout.
+<p>Ada beberapa atribut di controller CakePHP yang memberikan anda kontrol bagaimana views anda diset didalam sebuah layout.
 </p> </p>
 <p> <p>
- The <code>$layout</code> attribute can be set to the name of a layout saved in <kbd>/app/views/layouts</kbd>. You specify a layout by setting <code&gt;$layout</code> equal to the name of the layout file minus the <kbd>.ctp</kbd> extension. If this attribute has not been defined, CakePHP renders the default layout, <kbd>default.ctp</kbd>. If you haven’t defined one at <kbd>/app/views/layouts/default.ctp</kbd>, CakePHP’s core default layout will be rendered. + Atribut <code>$layout</code> dapat diset dengan nama dari layout yang disimpan di dalam <code>/app/views/layouts</code>. Anda dapat menentukan sebuah layout dengan menset $layout sesuai dengan nama berkas (file) layout, tanpa ekstensi .ctp. Jika atribut ini tidak didefinisikan, maka CakePHP akan merender layout default, <code>default.ctp</code>. Jika anda belum membuat file layout default di <code>/app/views/layouts/default.ctp</code>, maka layout default dari inti CakePHP-lah yang akan dirender.
 </p> </p>
 <pre> <pre>
 &lt;?php &lt;?php
-// Using $layout to define an alternate layout +// Menggunakan $layout untuk mendefinisikan layout alternatif
 class RecipesController extends AppController { class RecipesController extends AppController {
  function quickSave() {  function quickSave() {
  $this-&gt;layout = 'ajax';  $this-&gt;layout = 'ajax';
Lines: 17-30Lines: 16-30
 } }
 ?&gt; ?&gt;
 </pre> </pre>
-<p>You can also change the title of the page (that is located in the bar at the top of your browser) using <code&gt;$pageTitle</code>. In order for this to work properly, your layout needs to include the <code>$title_for_layout</code> variable, at least between the &lt;code&gt;&lt;title&gt;</code> and <code>&lt;/title&gt;</code> tags in the head of the HTML document. +<p> /> Anda juga bisa merubah &lt;em>title&lt;/em&gt; (judul) dari sebuah halaman (terletak di atas <em>bar&lt;/em&gt; dari perambah (browser) anda) dengan menggunakan $pageTitle. Agar hal ini dapat bekerja dengan benar, layout anda harus memasukkan variabel <code>$title_for_layout</code>, yang diletakkan di antara tag &lt;title&gt; dan &lt;/title&gt; pada bagian awal dokumen HTML.
 </p> </p>
 <pre> <pre>
 &lt;?php &lt;?php
-// Using $pageTitle to define the page title +// Menggunakan $pageTitle untuk mendefinisikan judul halaman
 class RecipesController extends AppController { class RecipesController extends AppController {
  function quickSave() {  function quickSave() {
  $this-&gt;pageTitle = 'My search engine optimized title';  $this-&gt;pageTitle = 'My search engine optimized title';
Lines: 33-41Lines: 33-41
 ?&gt; ?&gt;
 </pre> </pre>
 <p> <p>
- You can also set the page title from the view using <code>$this-&amp;gt;pageTitle</code> (You must include the <code>$this-&amp;gt;</code> part.) This is recommended, as it better separates the logic from the layout and content. For a static page you must use <code>$this-&amp;gt;pageTitle</code> in the view if you want a custom title. + Anda juga bisa menset judul halaman dari dalam controller menggunakan <code>$this->pageTitle</code> (Anda harus memasukkan bagian <code>$this-></code>). Hal ini direkomendasikan, karena akan lebih baik untuk memisahkan bagian logika dari layout dan isi (content). Untuk halaman statis, anda harus mendefinisikan $this->pageTitle di dalam view untuk mendapatkan judul kustom (yang sesuai).
 </p> </p>
 <p> <p>
- If <code>$this-&amp;gt;pageTitle</code> is not set, a title will be automatically generated based on the controller name, or the view file name in the case of a static page. + Jika <code>$this->pageTitle</code> tidak diset, maka judul halaman otomatis diganti dengan judul berdasarkan nama controller. Jika merupakan halaman statis, maka nama dari berkas (file) view lah yang akan digunakan sebagai judul.
 </p> </p>