CakePHP Notes ――view Layer 2

Source: Internet
Author: User
CakePHP Notes ――view Layer 2
CakePHP's view layer can be composed of different parts. Each part has a different purpose.
  • Views: The view is the only part of the page where the action runs. They form the response of the application.
  • elements: Small reusable View Code. Components are typically rendered inside the view.
  • layouts: Some view interface files in the application that are packaged with the presentation logic. Most views are rendered inside the layout.
  • Helpers: These classes wrap the view logic that is used in many places in the view layer. In addition to other things, CakePHP's assistants help you build forms, build AJAX features, paging model data, or provide RSS feeds.


    Working with view blocks

    The view block is placed in the $scripts _for_layoutand provides a flexible API that allows you to define slots or blocks anywhere in the view/layout. Blocks are an ideal way to implement something like a sidebar, or a good place to load resources in the head/Surga of a layout. Blocks are defined in two ways: as capturing blocks, or by directly assigning values. The start (), append () , and end () methods work together with the capture block:

    1 //Create a sidebar block2 $this->start (' sidebar ');3 Echo $this->element (' sidebar/recent_topics ');4 Echo $this->element (' sidebar/recent_comments ');5 $this-End();6 8 //Add a sidebar later9 $this->append (' sidebar ');Ten Echo $this->element (' sidebar/popular_topics '); One $this-End();

    You can also use start () multiple times to add a block. You can use assign () to clear or overwrite a block at any time:

    1 // clears the contents of the previously defined sidebar block.  2$this->assign (' sidebar ', ');

    In version 2.3, several new methods were added to work with blocks. prepend () presets The contents of an existing block:

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

    The Startifempty () method generates a block when either a block is empty or undefined. If the block already exists, the content defined by the startifempty () is ignored. You can use this method when you want to define default content for a block when it does not exist :

    1 //in the view file. 2 //Create a navigation bar block. 3 $this->startifempty (' NavBar ');4 Echo $this->element (' NavBar ');5 Echo $this->element (' Notifications ');6 $this-End();7 8 //in parent view/layout. 9 $this->startifempty (' NavBar ');Ten DefaultContent One $this-End(); A  - Echo $this->fetch (' NavBar ');

    In the example above, thenavbar block contains the content added in the first section. Once the block is defined in a child view, its default content is ignored.

    Display block

    You can use the fetch () method to display blocks. fetch will safely output a block, and if the block does not exist, return '.

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

    You can also decide whether to display its contents depending on whether a block exists. This is useful when you want to have a conditional display header or other label in the layout, inheritance view file:

    1 // in the APP/VIEW/LAYOUTS/DEFAULT.CTP  
     2 
         if ($this->fetch (' menu ')):?>3 class= "Menu" >4     

    Menu Options

    5 $this->fetch (' menu ');?>67 endif;?>

    In version 2.3.0, you can also provide default values for blocks when they do not have content. This makes it easier to add placeholders for empty states. You can use two parameters to provide a default value:

    1 class= "Shopping-cart" >2     

    Your Cart

    3 $this->fetch (' cart ', ' Your cart is empty '); 4

    Using script and CSS file blocks

    The block replaces the deprecated $scripts _for_layout layout variables. The htmlhelper is associated to the view block, and its script ( ), css () , and Meta () methods are The C8>false option is used together to update a block using the same name as the same one.

    1 
       2 //in the view file. 3 $this->html->script (' Carousel ',Array(' inline ' =false));4 $this->html->css (' Carousel ',NULL,Array(' inline ' =false));5?>6 7 //in the layout file. 8 9 Ten      One     <title><?php <span style= "Color:rgb (0,0,255)" >echo</span> <span style= "Color:rgb (128,0,128)" > $this </span>->fetch (' title ');?></title> A       
       Echo $this->fetch (' script ');?> -       
       Echo $this->fetch (' CSS ');?> -      the     //here is the rest of the layout countenance ...

    HtmlHelper also allows you to control which scripts and CSS blocks to use:

    1 // in the view file.  2$thisArray(' block ' = ' scriptbottom ')); 3 4 // in the layout file.  5echo$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.