cakephp札记――view层2

WBOY
發布: 2016-06-13 12:38:13
原創
858 人瀏覽過

cakephp笔记――view层2
CakePHP 的视图层可以由不同的部分构成。每一部分有不同的用途。

  • views:视图是动作运行的唯一的页面部分。它们构成了应用程序的响应。
  • elements:小的可重用的视图代码。元件通常在视图内部渲染。
  • layouts: 应用程序中打包了呈献逻辑的一些视图接口文件。多数视图在布局内部渲染。
  • helpers:这些类包装了视图层的许多地方都使用的视图逻辑。除了其它事项,CakePHP 的助手帮助你建立表单、构建 AJAX 功能、分页模型数据,或者提供 RSS feed。


    使用视图块

    视图块放在 $scripts_for_layout,并提供一个允许你在视图/布局中任意位置定义插槽或者块的灵活的 API。 块是实现类似边栏这样的东东的理想方法,或者是在布局的头/尾加载资源的好地方。块有两种定义方式:作为捕获块,或者通过直接赋值。start()、 append() 和 end() 方法是和捕获块一同工作的:

    <span style="color:rgb(0,128,128)"> 1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 建立一个边栏块</span>
    <span style="color:rgb(0,128,128)"> 2</span> <span style="color:rgb(128,0,128)">$this</span>->start('sidebar');
    <span style="color:rgb(0,128,128)"> 3</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->element('sidebar/recent_topics');
    <span style="color:rgb(0,128,128)"> 4</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->element('sidebar/recent_comments');
    <span style="color:rgb(0,128,128)"> 5</span> <span style="color:rgb(128,0,128)">$this</span>-><span style="color:rgb(0,128,128)">end</span>();
    <span style="color:rgb(0,128,128)"> 6</span> 
    <span style="color:rgb(0,128,128)"> 8</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 随后添加一个边栏</span>
    <span style="color:rgb(0,128,128)"> 9</span> <span style="color:rgb(128,0,128)">$this</span>->append('sidebar');
    <span style="color:rgb(0,128,128)">10</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->element('sidebar/popular_topics');
    <span style="color:rgb(0,128,128)">11</span> <span style="color:rgb(128,0,128)">$this</span>-><span style="color:rgb(0,128,128)">end</span>();
    登入後複製

    也可以多次使用 start() 添加进一个块。 任何时候都可以使用 assign() 清除或者覆盖一个块:

    <span style="color:rgb(0,128,128)">1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 清除之前定义的边栏块的内容。</span>
    <span style="color:rgb(0,128,128)">2</span> <span style="color:rgb(128,0,128)">$this</span>->assign('sidebar', '');
    登入後複製

    在 2.3 版本中,新加了几个与块一同工作的方法。prepend() 预置一个已存在的块的内容:

    <span style="color:rgb(0,128,128)">1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 预置到边栏。</span>
    <span style="color:rgb(0,128,128)">2</span> <span style="color:rgb(128,0,128)">$this</span>->prepend('sidebar', 'this content goes on top of sidebar');
    登入後複製

    startIfEmpty() 方法在一个块为空或者未定义时生成一个块。如果块已经存在,则 startIfEmpty() 定义的内容被忽略。当你想要在块不存在时为其定义默认内容时,可以使用这一方法::

    <span style="color:rgb(0,128,128)"> 1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在视图文件中。
    </span><span style="color:rgb(0,128,128)"> 2</span> <span style="color:rgb(0,128,0)">// 创建一个导航栏块。</span>
    <span style="color:rgb(0,128,128)"> 3</span> <span style="color:rgb(128,0,128)">$this</span>->startIfEmpty('navbar');
    <span style="color:rgb(0,128,128)"> 4</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->element('navbar');
    <span style="color:rgb(0,128,128)"> 5</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->element('notifications');
    <span style="color:rgb(0,128,128)"> 6</span> <span style="color:rgb(128,0,128)">$this</span>-><span style="color:rgb(0,128,128)">end</span>();
    <span style="color:rgb(0,128,128)"> 7</span> 
    <span style="color:rgb(0,128,128)"> 8</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在父视图/布局中。</span>
    <span style="color:rgb(0,128,128)"> 9</span> <span style="color:rgb(128,0,128)">$this</span>->startIfEmpty('navbar');
    <span style="color:rgb(0,128,128)">10</span> <span style="color:rgb(0,0,255)">Default</span> content
    <span style="color:rgb(0,128,128)">11</span> <span style="color:rgb(128,0,128)">$this</span>-><span style="color:rgb(0,128,128)">end</span>();
    <span style="color:rgb(0,128,128)">12</span> 
    <span style="color:rgb(0,128,128)">13</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->fetch('navbar');
    登入後複製

    上面的例子中,navbar 块包含在第一部分中添加的内容。一旦在子视图中定义了这个块,其默认内容将被忽略。

    显示块

    可以使用 fetch() 方法显示块。 fetch 将安全地输出一个块,如果块不存在,就返回 ‘’。

    <span style="color:rgb(0,128,128)">1</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->fetch('sidebar');
    登入後複製

    还可以根据一个块是否存在来决定是否显示其内容。要想在布局、继承视图文件中有条件的显示头或者其它标签时,这种方法非常有用:

    <span style="color:rgb(0,128,128)">1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在 app/View/Layouts/default.ctp 中</span>
    <span style="color:rgb(0,128,128)">2</span> <?php <span style="color:rgb(0,0,255)">if (<span style="color:rgb(128,0,128)">$this</span>->fetch('menu')): ?>
    <span style="color:rgb(0,128,128)">3</span> <div style="color:rgb(0,0,255)">class="menu">
    <span style="color:rgb(0,128,128)">4</span>     <h3>Menu options</h3>
    <span style="color:rgb(0,128,128)">5</span> <?php <span style="color:rgb(0,0,255)">echo <span style="color:rgb(128,0,128)">$this</span>->fetch('menu'); ?>
    <span style="color:rgb(0,128,128)">6</span> </div>
    <span style="color:rgb(0,128,128)">7</span> <?php <span style="color:rgb(0,0,255)">endif; ?>
    登入後複製

    在 2.3.0 版,还可以在块没有内容时为其提供默认值。这使为空状态添加占位符变得更容易。可以使用两个参数提供默认值:

    <span style="color:rgb(0,128,128)">1</span> <div style="color:rgb(0,0,255)">class="shopping-cart">
    <span style="color:rgb(0,128,128)">2</span>     <h3>Your Cart</h3>
    <span style="color:rgb(0,128,128)">3</span> <?php <span style="color:rgb(0,0,255)">echo <span style="color:rgb(128,0,128)">$this</span>->fetch('cart', 'Your cart is empty');
    <span style="color:rgb(0,128,128)">4</span> </div>
    登入後複製

    使用 script 和 CSS 文件块

    块替代了被废弃的 $scripts_for_layout 布局变量。HtmlHelper 关联到视图块,它的 script() 、 css() 和 meta()方法在与 inline = false 选项共同使用时使用相同的相同的名字更新一个块。

    <span style="color:rgb(0,128,128)"> 1</span> <?php <span style="color:rgb(0,128,128)"> 2 <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在视图文件中。</span>
    <span style="color:rgb(0,128,128)"> 3</span> <span style="color:rgb(128,0,128)">$this</span>->Html->script('carousel', <span style="color:rgb(0,0,255)">array</span>('inline' => <span style="color:rgb(0,0,255)">false</span>));
    <span style="color:rgb(0,128,128)"> 4</span> <span style="color:rgb(128,0,128)">$this</span>->Html->css('carousel', <span style="color:rgb(0,0,255)">null</span>, <span style="color:rgb(0,0,255)">array</span>('inline' => <span style="color:rgb(0,0,255)">false</span>));
    <span style="color:rgb(0,128,128)"> 5</span> ?>
    <span style="color:rgb(0,128,128)"> 6</span> 
    <span style="color:rgb(0,128,128)"> 7</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在布局文件中。</span>
    <span style="color:rgb(0,128,128)"> 8</span> 
    <span style="color:rgb(0,128,128)"> 9</span> 
    <span style="color:rgb(0,128,128)">10</span>     
    <span style="color:rgb(0,128,128)">11</span>     <title>
    <?php <span style="color:rgb(0,0,255)">echo <span style="color:rgb(128,0,128)">$this</span>->fetch('title'); ?></title>
    <span style="color:rgb(0,128,128)">12</span> <span style="white-space:pre">	</span>      <?php <span style="color:rgb(0,0,255)">echo <span style="color:rgb(128,0,128)">$this</span>->fetch('script'); ?>
    <span style="color:rgb(0,128,128)">13</span> <span style="white-space:pre">	</span>      <?php <span style="color:rgb(0,0,255)">echo <span style="color:rgb(128,0,128)">$this</span>->fetch('css'); ?>
    <span style="color:rgb(0,128,128)">14</span>     
    <span style="color:rgb(0,128,128)">15</span>     <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 下面是剩余的布局尊容...</span>
    登入後複製

    HtmlHelper 还允许你控制使用哪个 scripts 和 CSS 块:

    <span style="color:rgb(0,128,128)">1</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在视图文件中。</span>
    <span style="color:rgb(0,128,128)">2</span> <span style="color:rgb(128,0,128)">$this</span>->Html->script('carousel', <span style="color:rgb(0,0,255)">array</span>('block' => 'scriptBottom'));
    <span style="color:rgb(0,128,128)">3</span> 
    <span style="color:rgb(0,128,128)">4</span> <span style="color:rgb(0,128,0)">//</span><span style="color:rgb(0,128,0)"> 在布局文件中。</span>
    <span style="color:rgb(0,128,128)">5</span> <span style="color:rgb(0,0,255)">echo</span> <span style="color:rgb(128,0,128)">$this</span>->fetch('scriptBottom');
    登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!