cakephp札记――view层2

WBOY
Freigeben: 2016-06-13 12:38:13
Original
857 Leute haben es durchsucht

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>();
    Nach dem Login kopieren

    也可以多次使用 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', '');
    Nach dem Login kopieren

    在 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');
    Nach dem Login kopieren

    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');
    Nach dem Login kopieren

    上面的例子中,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');
    Nach dem Login kopieren

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

    <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; ?>
    Nach dem Login kopieren

    在 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>
    Nach dem Login kopieren

    使用 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>
    Nach dem Login kopieren

    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');
    Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!