Cakephp notes -- view layer _ PHP Tutorial

Source: Internet
Author: User
Cakephp notes-view layer. A view is a page template, usually named after the action. For example, the PostsController: add view is located in appviewspostsadd. ctp. Cakeview is a very simple php file. Therefore, a view is a page template named by action. For example, the PostsController: add view is located in/app/views/posts/add. ctp. Cake view is a very simple PHP file, so you can use any PHP code in it.

Layouts layout

A layout contains the presentation code of all packages outside the view. Layout files are stored in the/app/views/layouts directory. You can override a default layout to replace the default layout of Cake in/app/views/layouts/default. ctp. 1. a new default layout is created. when the page is render, the content of the controller view will be replaced with the default layout. If needed, you can create any number of layout for your Cake site, as long as you put them in the app/views/layouts Directory, in addition, you can use the $ layout variable or setLayout () method of the controller in your controller action to switch to layout.

Elements element

Element elements are generated mainly for code or code block reuse. Many applications have repeated presentation layer code on different pages. sometimes they are only in different places in layout. Cake can help you copy some blocks in the site if necessary. These reusable page blocks are called elements ). Advertisements, help boxes, navigation bars, menus, and illustrations are all implemented as elements by Cake. An element can be considered as a mini-view that can be included in other views. All elements are stored in the/app/views/elements directory, and the file extension is. ctp.

By default, element cannot access any data. To make it have access to data, you need to put the data in an array and pass it to it. do not forget to name the data (key-value pair array ).

Call an element without parameters

RenderElement ('helpbox');?>

Call an element and input an array containing data.

$ This-> renderElement ('helpbox', array (

"Helptext" => "Oh, this text is very helpful .")

);

?>

In the element file, all input variables can be used by their key names in the parameter array (a bit similar to the variables set by controller in view ). In the above example, the $ helptext variable can be used in the/app/views/elements/helpbox. ctp file. Of course, if an array is passed to the element, it will contain more information. Element makes the view more readable, and places the part of the repeat element of the render in the corresponding element file. Of course, they can also help you reuse the content blocks in the site.

Bytes. For example, the PostsController: add view is located in/app/views/posts/add. ctp. Cake view is a very simple php file, so you...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.