Yii2 source code learning notes (19th), yii2 source code learning notes. Yii2 source code learning notes (19th), yii2 source code learning notes view remaining code 1 *** @ returnstring | booleantheviewfilecurrentlybeingrendered. Falseifnoviewfileisbeing yii2 source code learning notes (19th)
View remaining code
1/** 2 * @ return string | boolean the view file currently being rendered. false if no view file is being rendered. 3 * currently rendered view file 4 */5 public function getViewFile () 6 {7 return end ($ this-> _ viewFiles ); 8} 9 10/** 11 * This method is invoked right before [[renderFile ()] renders a view file. 12 * The default implementation will trigger the [[EVENT_BEFORE_RENDER] event. 13 * run [renderFile () [[EVENT_BEFORE_RENDER] event 14 * If you override this method, make sure you call the parent implementation first. 15 * @ param string $ viewFile the view file to be rendered. view file to be rendered. 16 * @ param array $ params the parameter array passed to the [[render ()] method. 17 * parameter array is passed to the [render ()] method. 18 * @ return boolean whether to continue rendering the view file. whether to continue rendering the view file. 19 */20 public function beforeRender ($ viewFile, $ params) 21 {22 $ event = new ViewEvent ([// instantiate ViewEvent 23 'viewfile' => $ viewFile, 24 'params' => $ params, 25]); 26 $ this-> trigger (self: EVENT_BEFORE_RENDER, $ event ); // trigger the [EVENT_BEFORE_RENDER] event 27 28 return $ event-> isValid; // determine whether to continue rendering the file 29} 30 31/** 32 * This method is invoked right after [[renderFile ()] renders a view file. 33 * The default imp Lementation will trigger the [[EVENT_AFTER_RENDER] event. 34 * Post-events are called after the [renderFile ()] method is executed. by default, [[EVENT_AFTER_RENDER] event 35 * If you override this method, make sure you call the parent implementation first. 36 * @ param string $ viewFile the view file being rendered. view file to be rendered. 37 * @ param array $ params the parameter array passed to the [[render ()] method. 38 * parameter array is passed to the [render ()] method. 39 * @ param string $ output the rendering result of the view file. updates to this parameter 40 * will be passed back and returned by [[renderFile ()]. 41 * returned view rendering result 42 */43 public function afterRender ($ viewFile, $ params, & $ output) 44 {45 if ($ this-> hasEventHandlers (self :: EVENT_AFTER_RENDER) {// Determine whether the [EVENT_AFTER_RENDER] event exists 46 $ event = new ViewEvent ([47 'viewfile' => $ viewFile, 48 'params' => $ Params, 49 'output' => $ output, 50]); 51 // trigger the [EVENT_AFTER_RENDER] event 52 $ this-> trigger (self: EVENT_AFTER_RENDER, $ event ); 53 $ output = $ event-> output; // return result 54} 55} 56 57/** 58 * Renders a view file as a PHP script. 59 * return a view file as a PHP script 60 * This method treats the view file as a PHP script and includes des the file. 61 * It extracts the given parameters and makes them available in the view file. 62 * The method captures the output of the specified Ded view file and returns it as a string. 63 * convert the input parameter to a variable, including and executing the view file, and return the execution result 64 * This method shoshould mainly be called by view renderer or [[renderFile ()]. 65*66 * @ param string $ _ file _ the view file. view file 67 * @ param array $ _ params _ the parameters (name-value pairs) that will be extracted and made available in the view file. 68 * @ return string Rendering result execution result 69 */70 public function renderPhpFile ($ _ file _, $ _ params _ = []) 71 {72 ob_start (); // enable the output buffer 73 ob_implicit_flush (false); // disable the buffer 74 extract ($ _ params _, EXTR_OVERWRITE ); // Convert an array to a variable using 75 require ($ _ file _); 76 77 return ob_get_clean (); // Get the buffer content and clear the current output buffer 78} 79 80/** 81 * Renders dynamic content returned by the given PHP statements. rendering dynamic content 82 * This method is mainly used tog Ether with content caching (fragment caching and page caching) 83 * used to aggregate cached content 84 * when some portions of the content (called * dynamic content *) shoshould not be cached. 85 * The dynamic content must be returned by some PHP statements. 86 * render dynamic content returned by some PHP statements. 87 * @ param string $ statements the PHP statements for generating the dynamic content. PHP statements that generate dynamic content. 88 * @ return string the placeholder of the dynamic content, or the dynamic content if there is no 89 * active content cache currently. dynamic content placeholder if no valid content cache exists currently, call evaluateDynamicContent to output 90 */91 public function renderDynamic ($ statements) 92 {93 if (! Empty ($ this-> cacheStack) {// The dynamic content list is not empty 94 $ n = count ($ this-> dynamicPlaceholders ); // count the number of dynamic content items 95 $ placeholder ="YII-DYNAMIC-$n"; // Generate a placeholder 96 $ this-> addDynamicPlaceholder ($ placeholder, $ statements); // add a dynamic content placeholder 97 98 return $ placeholder; 99} else {// The incoming PHP statement is not effectively cached and the execution result 100 return $ this-> evaluateDynamicContent ($ statements) is returned ); 101} 102} 103 104/** 105 * Adds a placeholder for dynamic content. add a dynamic content placeholder, 106 * This method is internally used. internally, use 107 * @ param string $ placeholder the placeholder name 108 * @ param string $ st. Atements the PHP statements for generating the dynamic content109 * PHP statement for generating dynamic content 110 */111 public function addDynamicPlaceholder ($ placeholder, $ statements) 112 {113 foreach ($ this-> cacheStack as $ cache) {114 $ cache-> dynamicPlaceholders [$ placeholder] = $ statements; // add a dynamic content placeholder 115} 116 $ this-> dynamicPlaceholders [$ placeholder] = $ statements; // add a dynamic content placeholder 117} 118 119/** 120 * Evaluates the given PHP s to the current view Tatements. the given PHP statement value is 121 * This method is mainly used internally to implement dynamic content feature. internal use of the dynamic content function 122 * @ param string $ statements the PHP statements to be evaluated. PHP statement 123 * @ return mixed the return value of the PHP statements. PHP statement value 124 */125 public function evaluateDynamicContent ($ statements) 126 {127 return eval ($ statements); 128} 129 130/** 131 * Begins recording a block. 132 * This method is a starting cut to beginning [[Block] 133 * mark of start of data Block, this method is to start the [Block] shortcut 134 * data blocks can be specified in one place to display the view content in another place, usually 135 * @ param string $ id the block ID is used with the layout. data block ID 136 * @ param boolean $ renderInPlace whether to render the block content in place. whether to render block content. 137 * Defaults to false, meaning the captured block will not be displayed.138 * @ return Block the Block widget instance data block part instance 139 */140 public function beginBlock ($ id, $ renderInPlace = false) 141 {142 return Block: begin ([143 'id' => $ id, // the unique identifier of the data Block is 144 'renderinplace '=> $ renderInPlace, // whether to display the identifier 145 'View' => $ this, 146]); 147} 148 149/** 150 * Ends recording a block. data block end id 151 */152 public function en DBlock () 153 {154 Block: end (); 155} 156 157/** 158 * Begins the rendering of content that is to be decorated by the specified view.159 * This method can be used to implement nested layout. for example, a layout can be embedded160 * in another layout file specified as '@ app/views/layouts/base. php 'like the following: 161 * specifies the view rendering content to implement nested layout. the first input parameter is the layout file path 162 *~~~ 163 *
BeginContent ('@ app/views/layouts/base. php');?> 164 *... layout content here... 165 *
EndContent () ;?> 166 *~~~ 167*168 * @ param string $ viewFile the view file that will be used to decorate the content enclosed by this widget.169 * This can be specified as either the view file path or path alias. the path or path alias of the layout file. 170 * @ param array $ params the variables (name => value) to be extracted and made available in the decorative view.171 * parameter 172 * @ return ContentDecorator the ContentDecorator widget instance 173 * @ see ContentDecorator174 */175 public function beginContent ($ viewFile, $ params = []) 176 {177 return ContentDecorator: begin ([178 'viewfile' => $ viewFile, 179 'params' => $ params, 180 'View' => $ This, 181]); 182} 183 184/** 185 * Ends the rendering of content. end rendering content 186 */187 public function endContent () 188 {189 ContentDecorator: end (); 190} 191 192/** 193 * Begins fragment caching. start fragment cache 194 * This method will display cached content if it is available.195 * If not, it will start caching and wowould keep Ct an [[endCache ()] 196 * call to end the cache and save the content into cache.197 * available Otherwise, the cached content will start until the [endCache ()] method 198 * A typical usage of fragment caching is as follows, 199*200 *~~~ 201 * if ($ this-> beginCache ($ id) {202 *//... generate content here203 * $ this-> endCache (); 204 *} 205 *~~~ 206*207 * @ param string $ id a unique ID identifying the fragment to be cached. the unique identifier 208 * @ param array $ properties initial property values for [[FragmentCache] initial attribute [FragmentCache] 209 * @ return boolean whether you shoshould generate the content for caching. whether to generate cached content. 210 * False if the cached version is available.211 */212 public function beginCache ($ id, $ properties = []) 213 {214 $ properties ['id'] = $ id; // fragment ID 215 $ properties ['View'] = $ this; // call the initialization attribute 216/* @ var $ cache FragmentCache */217 $ cache = FragmentCache :: begin ($ properties); 218 if ($ cache-> getCachedContent ()! = False) {219 $ this-> endCache (); // read the cached content from the cache, render the content and return false. 220 return false is not cached; 222} else {223 return true; 224} 225} 226 227/** 228 * Ends fragment caching. end FragmentCache 229 */230 public function endCache () 231 {232 FragmentCache: end (); 233} 234 235/** 236 * Marks the beginning of a page. mark 237 */238 public function beginPage () 239 {240 ob_start (); // enable the output buffer 241 ob_implicit_flush (false ); // disable the buffer 242 $243 $ this-> trigger (self: EVENT_BEGIN_PAGE); 244} 245 246/** 247 * Marks the ending of a page. page end Mark 248 */249 public function endPage () 250 {251 $ this-> trigger (self: EVENT_END_PAGE); 252 ob_end_flush (); // Close output buffer 253}
Except (19th), yii2 source code learning note view remaining code 1/*** 2 * @ return string | boolean the view file currently being rendered. False if no view file is being...