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