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');

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools