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 id="Menu-options">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 id="Your-Cart">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和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。