Cakephp notes -- view layer 2

Source: Internet
Author: User
Cakephp notes-view layer 2CakePHP view layer can be composed of different parts. Each part has different purposes. Views: a view is the only page part of an action. They constitute the response of the application. Elements: small reusable View code. Components are usually rendered inside the view. Layouts: cakephp notes -- view layer 2
The view layer of CakePHP can be composed of different parts. Each part has different purposes.
  • Views: A view is the only page part of an action. They constitute the response of the application.
  • Elements: Small reusable View code. Components are usually rendered inside the view.
  • Layouts: Some view interface files that present logic are packaged in the application. Most views are rendered inside the layout.
  • Helpers: These classes encapsulate the View logic used in many places on the view layer. In addition to other things, CakePHP's assistant helps you create forms, build AJAX functions, paging model data, or provide RSS feeds.
    Use view blocks

    View block placement$ Scripts_for_layoutAnd provides a flexible API that allows you to define slots or blocks anywhere in the view/Layout. A block is an ideal way to implement something like a sidebar, or a good place to load resources at the head/end of the layout. A block can be defined in two ways: as a capture block or directly assigned a value.Start (),Append ()AndEnd ()The method works with the capture block:

    1 // Create a sidebar 2 $ this-> start ('inclubar'); 3 echo $ this-> element ('sidebar/recent_topics '); 4 echo $ this-> element ('sidebar/recent_comments '); 5 $ this-> end (); 6 8 // then add a sidebar 9 $ this-> append ('inclubar'); 10 echo $ this-> element ('sidebar/popular_topics '); 11 $ this-> end ();

    It can also be used multiple timesStart ()Add a block. Can be used at any timeAssign ()Clear or overwrite a block:

    1 // clear the content of the previously defined sidebar. 2 $ this-> assign ('inclubar ','');

    In version 2.3, several new methods are added to work with blocks.Prepend ()Preset the content of an existing block:

    1 // preset to the sidebar. 2 $ this-> prepend ('includebar', 'This content goes on top of includebar ');

    StartIfEmpty ()Method to generate a block when a block is empty or undefined. If the block already existsStartIfEmpty ()The defined content is ignored. When youWhen you want to define the default content for a block when it does not exist, You can use this method ::

    1 // In the view file. 2 // create a navigation block. 3 $ this-> startIfEmpty ('navbar'); 4 echo $ this-> element ('navbar'); 5 echo $ this-> element ('notification '); 6 $ this-> end (); 7 8 // in The Parent View/Layout. 9 $ this-> startIfEmpty ('navbar'); 10 Default content11 $ this-> end (); 12 13 echo $ this-> fetch ('navbar ');

    In the above example,NavbarThe block contains the content added in the first part. Once this block is defined in the subview, its default content will be ignored.

    Display block

    AvailableFetch ()Method display block.FetchOutputs a block safely. if the block does not exist, ''is returned ''.

    1 echo $this->fetch('sidebar');

    You can also determine whether to display the content based on whether a block exists. To layout and inherit view filesConditional display header or other labelsThis method is very useful:

    1 // In app/View/Layouts/default. ctp 2
       Fetch ('menu '):?> 3 class = "menu"> 4 Menu options5
       Fetch ('menu ');?> 6 7
       

    In version 2.3.0, you can also provide the default value when the block has no content. This makes it easier to add placeholders for null states. You can use two parameters to provide default values:

    1 class="shopping-cart">2     Your Cart3 
       fetch('cart', 'Your cart is empty');4 

    Use script and CSS file blocks

    Blocks replace discarded$ Scripts_for_layoutLayout variable.HtmlHelperAssociated with the view block, itsScript (),Css ()AndMeta ()The method isInline = falseTo update a block with the same name.

    1
       Html-> script ('invalid usel', array ('inline' => false); 4 $ this-> Html-> css ('invalid usel', null, array ('line' => false); 5?> 6 7 // in the layout file. 891011<? Php echo $ this-> fetch ('title');?>12
       Fetch ('script');?> 13
       Fetch ('css ');?> 1415 // The remaining layout is shown below...

    HtmlHelperYou can also control which scripts and CSS block to use:

    1 // In the view file. 2 $ this-> Html-> script ('initusel', array ('block' => 'scriptbottom '); 3 4 // in the layout file. 5 echo $ this-> fetch ('scriptbottom ');

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.