yii2 source code study notes (19), yii2 source code study notes
Jul 06, 2016 pm 02:24 PMyii2 source code study notes (19), yii2 source code study notes
view the remaining code
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * @return string|boolean the view file currently being rendered. False if no view file is being rendered. </span><span> 3</span> <span> * 當前正在渲染的視圖文件 </span><span> 4</span> <span>*/</span> <span> 5</span> <span>public</span><span> function getViewFile() </span><span> 6</span> <span> { </span><span> 7</span> <span>return</span> end($<span>this</span>-><span>_viewFiles); </span><span> 8</span> <span> } </span><span> 9</span> <span> 10</span> <span>/*</span><span>* </span><span> 11</span> <span> * This method is invoked right before [[renderFile()]] renders a view file. </span><span> 12</span> <span> * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. </span><span> 13</span> <span> * 前置事件,執(zhí)行[renderFile()]時被調用,默認觸發(fā)[[EVENT_BEFORE_RENDER]]事件 </span><span> 14</span> <span> * If you override this method, make sure you call the parent implementation first. </span><span> 15</span> <span> * @param string $viewFile the view file to be rendered. 要渲染的視圖文件。 </span><span> 16</span> <span> * @param array $params the parameter array passed to the [[render()]] method. </span><span> 17</span> <span> * 參數數組傳遞到[render()]方法。 </span><span> 18</span> <span> * @return boolean whether to continue rendering the view file. 是否繼續(xù)渲染視圖文件。 </span><span> 19</span> <span>*/</span> <span> 20</span> <span>public</span> function beforeRender($viewFile, $<span>params</span><span>) </span><span> 21</span> <span> { </span><span> 22</span> $<span>event</span> = <span>new</span> ViewEvent([<span>//</span><span>實例化ViewEvent</span> <span> 23</span> <span>'</span><span>viewFile</span><span>'</span> =><span> $viewFile, </span><span> 24</span> <span>'</span><span>params</span><span>'</span> => $<span>params</span><span>, </span><span> 25</span> <span> ]); </span><span> 26</span> $<span>this</span>->trigger(self::EVENT_BEFORE_RENDER, $<span>event</span>);<span>//</span><span>觸發(fā)[EVENT_BEFORE_RENDER]事件</span> <span> 27</span> <span> 28</span> <span>return</span> $<span>event</span>->isValid;<span>//</span><span>判斷是否繼續(xù)渲染文件</span> <span> 29</span> <span> } </span><span> 30</span> <span> 31</span> <span>/*</span><span>* </span><span> 32</span> <span> * This method is invoked right after [[renderFile()]] renders a view file. </span><span> 33</span> <span> * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. </span><span> 34</span> <span> * 后置事件,在執(zhí)行[renderFile()]方法后被調用,默認觸發(fā)[[EVENT_AFTER_RENDER]]事件 </span><span> 35</span> <span> * If you override this method, make sure you call the parent implementation first. </span><span> 36</span> <span> * @param string $viewFile the view file being rendered.要渲染的視圖文件。 </span><span> 37</span> <span> * @param array $params the parameter array passed to the [[render()]] method. </span><span> 38</span> <span> * 參數數組傳遞到[render()]方法。 </span><span> 39</span> <span> * @param string $output the rendering result of the view file. Updates to this parameter </span><span> 40</span> <span> * will be passed back and returned by [[renderFile()]]. </span><span> 41</span> <span> * 返回視圖渲染的結果 </span><span> 42</span> <span>*/</span> <span> 43</span> <span>public</span> function afterRender($viewFile, $<span>params</span>, &<span>$output) </span><span> 44</span> <span> { </span><span> 45</span> <span>if</span> ($<span>this</span>->hasEventHandlers(self::EVENT_AFTER_RENDER)) {<span>//</span><span>判斷[EVENT_AFTER_RENDER]事件是否存在</span> <span> 46</span> $<span>event</span> = <span>new</span><span> ViewEvent([ </span><span> 47</span> <span>'</span><span>viewFile</span><span>'</span> =><span> $viewFile, </span><span> 48</span> <span>'</span><span>params</span><span>'</span> => $<span>params</span><span>, </span><span> 49</span> <span>'</span><span>output</span><span>'</span> =><span> $output, </span><span> 50</span> <span> ]); </span><span> 51</span> <span>//</span><span>觸發(fā)[EVENT_AFTER_RENDER]事件</span> <span> 52</span> $<span>this</span>->trigger(self::EVENT_AFTER_RENDER, $<span>event</span><span>); </span><span> 53</span> $output = $<span>event</span>->output;<span>//</span><span>返回結果</span> <span> 54</span> <span> } </span><span> 55</span> <span> } </span><span> 56</span> <span> 57</span> <span>/*</span><span>* </span><span> 58</span> <span> * Renders a view file as a PHP script. </span><span> 59</span> <span> * 返回一個視圖文件當作PHP腳本 </span><span> 60</span> <span> * This method treats the view file as a PHP script and includes the file. </span><span> 61</span> <span> * It extracts the given parameters and makes them available in the view file. </span><span> 62</span> <span> * The method captures the output of the included view file and returns it as a string. </span><span> 63</span> <span> * 將傳入的參數轉換為變量,包含并執(zhí)行view文件,返回執(zhí)行結果 </span><span> 64</span> <span> * This method should mainly be called by view renderer or [[renderFile()]]. </span><span> 65</span> <span> * </span><span> 66</span> <span> * @param string $_file_ the view file. 視圖文件 </span><span> 67</span> <span> * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. </span><span> 68</span> <span> * @return string the rendering result 執(zhí)行結果 </span><span> 69</span> <span>*/</span> <span> 70</span> <span>public</span> function renderPhpFile($_file_, $_params_ =<span> []) </span><span> 71</span> <span> { </span><span> 72</span> ob_start(); <span>//</span><span>打開輸出緩沖</span> <span> 73</span> ob_implicit_flush(<span>false</span>); <span>//</span><span>關閉緩沖區(qū)</span> <span> 74</span> extract($_params_, EXTR_OVERWRITE);<span>//</span><span> 將一個數組轉換為變量使用</span> <span> 75</span> <span> require($_file_); </span><span> 76</span> <span> 77</span> <span>return</span> ob_get_clean();<span>//</span><span>得到緩沖區(qū)的內容并清除當前輸出緩沖</span> <span> 78</span> <span> } </span><span> 79</span> <span> 80</span> <span>/*</span><span>* </span><span> 81</span> <span> * Renders dynamic content returned by the given PHP statements. 渲染動態(tài)內容 </span><span> 82</span> <span> * This method is mainly used together with content caching (fragment caching and page caching) </span><span> 83</span> <span> * 用來聚合緩存的內容 </span><span> 84</span> <span> * when some portions of the content (called *dynamic content*) should not be cached. </span><span> 85</span> <span> * The dynamic content must be returned by some PHP statements. </span><span> 86</span> <span> * 渲染某些被PHP語句返回的動態(tài)內容 </span><span> 87</span> <span> * @param string $statements the PHP statements for generating the dynamic content.生成動態(tài)內容的PHP語句。 </span><span> 88</span> <span> * @return string the placeholder of the dynamic content, or the dynamic content if there is no </span><span> 89</span> <span> * active content cache currently. 動態(tài)內容占位符 如果當前沒有有效的內容緩存,調用evaluateDynamicContent輸出 </span><span> 90</span> <span>*/</span> <span> 91</span> <span>public</span><span> function renderDynamic($statements) </span><span> 92</span> <span> { </span><span> 93</span> <span>if</span> (!empty($<span>this</span>->cacheStack)) {<span>//</span><span>動態(tài)內容的列表不為空</span> <span> 94</span> $n = count($<span>this</span>->dynamicPlaceholders);<span>//</span><span>統(tǒng)計動態(tài)內容條數</span> <span> 95</span> $placeholder = <span>"</span><span><![CDATA[YII-DYNAMIC-$n]]></span><span>"</span>;<span>//</span><span>生成占位符</span> <span> 96</span> $<span>this</span>->addDynamicPlaceholder($placeholder, $statements);<span>//</span><span>添加動態(tài)內容占位符</span> <span> 97</span> <span> 98</span> <span>return</span><span> $placeholder; </span><span> 99</span> } <span>else</span> {<span>//</span><span>沒有有效緩存 執(zhí)行傳入的PHP語句,返回執(zhí)行結果</span> <span>100</span> <span>return</span> $<span>this</span>-><span>evaluateDynamicContent($statements); </span><span>101</span> <span> } </span><span>102</span> <span> } </span><span>103</span> <span>104</span> <span>/*</span><span>* </span><span>105</span> <span> * Adds a placeholder for dynamic content. 添加一個動態(tài)內容占位符 </span><span>106</span> <span> * This method is internally used. 內部使用 </span><span>107</span> <span> * @param string $placeholder the placeholder name 占位符名稱 </span><span>108</span> <span> * @param string $statements the PHP statements for generating the dynamic content </span><span>109</span> <span> * 生成動態(tài)內容的PHP語句 </span><span>110</span> <span>*/</span> <span>111</span> <span>public</span><span> function addDynamicPlaceholder($placeholder, $statements) </span><span>112</span> <span> { </span><span>113</span> <span>foreach</span> ($<span>this</span>->cacheStack <span>as</span><span> $cache) { </span><span>114</span> $cache->dynamicPlaceholders[$placeholder] = $statements;<span>//</span><span>添加動態(tài)內容占位符</span> <span>115</span> <span> } </span><span>116</span> $<span>this</span>->dynamicPlaceholders[$placeholder] = $statements;<span>//</span><span>給當前視圖添加動態(tài)內容占位符</span> <span>117</span> <span> } </span><span>118</span> <span>119</span> <span>/*</span><span>* </span><span>120</span> <span> * Evaluates the given PHP statements. 給定的PHP語句的值 </span><span>121</span> <span> * This method is mainly used internally to implement dynamic content feature.內部使用實現(xiàn)動態(tài)內容功能 </span><span>122</span> <span> * @param string $statements the PHP statements to be evaluated. PHP語句進行計算 </span><span>123</span> <span> * @return mixed the return value of the PHP statements. PHP語句的值 </span><span>124</span> <span>*/</span> <span>125</span> <span>public</span><span> function evaluateDynamicContent($statements) </span><span>126</span> <span> { </span><span>127</span> <span>return</span><span> eval($statements); </span><span>128</span> <span> } </span><span>129</span> <span>130</span> <span>/*</span><span>* </span><span>131</span> <span> * Begins recording a block. </span><span>132</span> <span> * This method is a shortcut to beginning [[Block]] </span><span>133</span> <span> * 數據塊開始的標記,該方法是開始[Block]的快捷方式 </span><span>134</span> <span> * 數據塊可以在一個地方指定視圖內容在另一個地方顯示,通常和布局一起使用 </span><span>135</span> <span> * @param string $id the block ID. 數據塊標識 </span><span>136</span> <span> * @param boolean $renderInPlace whether to render the block content in place. 是否渲染塊內容。 </span><span>137</span> <span> * Defaults to false, meaning the captured block will not be displayed. </span><span>138</span> <span> * @return Block the Block widget instance 數據塊部件實例 </span><span>139</span> <span>*/</span> <span>140</span> <span>public</span> function beginBlock($id, $renderInPlace = <span>false</span><span>) </span><span>141</span> <span> { </span><span>142</span> <span>return</span><span> Block::begin([ </span><span>143</span> <span>'</span><span>id</span><span>'</span> => $id,<span>//</span><span>數據塊唯一標識</span> <span>144</span> <span>'</span><span>renderInPlace</span><span>'</span> => $renderInPlace,<span>//</span><span>是否顯示標識</span> <span>145</span> <span>'</span><span>view</span><span>'</span> => $<span>this</span><span>, </span><span>146</span> <span> ]); </span><span>147</span> <span> } </span><span>148</span> <span>149</span> <span>/*</span><span>* </span><span>150</span> <span> * Ends recording a block. 數據塊結束標識 </span><span>151</span> <span>*/</span> <span>152</span> <span>public</span><span> function endBlock() </span><span>153</span> <span> { </span><span>154</span> <span> Block::end(); </span><span>155</span> <span> } </span><span>156</span> <span>157</span> <span>/*</span><span>* </span><span>158</span> <span> * Begins the rendering of content that is to be decorated by the specified view. </span><span>159</span> <span> * This method can be used to implement nested layout. For example, a layout can be embedded </span><span>160</span> <span> * in another layout file specified as '@app/views/layouts/base.php' like the following: </span><span>161</span> <span> * 開始指定的view渲染內容,用來實現(xiàn)嵌套布局,傳入的第一個參數為布局文件的路徑 </span><span>162</span> <span> * ~~~ </span><span>163</span> <span> * <?php $this->beginContent('@app/views/layouts/base.php'); ?> </span><span>164</span> <span> * ...layout content here... </span><span>165</span> <span> * <?php $this->endContent(); ?> </span><span>166</span> <span> * ~~~ </span><span>167</span> <span> * </span><span>168</span> <span> * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget. </span><span>169</span> <span> * This can be specified as either the view file path or path alias.布局文件的路徑或路徑別名。 </span><span>170</span> <span> * @param array $params the variables (name => value) to be extracted and made available in the decorative view. </span><span>171</span> <span> * 可以在視圖中運用的參數 </span><span>172</span> <span> * @return ContentDecorator the ContentDecorator widget instance 部件實例 </span><span>173</span> <span> * @see ContentDecorator </span><span>174</span> <span>*/</span> <span>175</span> <span>public</span> function beginContent($viewFile, $<span>params</span> =<span> []) </span><span>176</span> <span> { </span><span>177</span> <span>return</span><span> ContentDecorator::begin([ </span><span>178</span> <span>'</span><span>viewFile</span><span>'</span> =><span> $viewFile, </span><span>179</span> <span>'</span><span>params</span><span>'</span> => $<span>params</span><span>, </span><span>180</span> <span>'</span><span>view</span><span>'</span> => $<span>this</span><span>, </span><span>181</span> <span> ]); </span><span>182</span> <span> } </span><span>183</span> <span>184</span> <span>/*</span><span>* </span><span>185</span> <span> * Ends the rendering of content.結束渲染內容 </span><span>186</span> <span>*/</span> <span>187</span> <span>public</span><span> function endContent() </span><span>188</span> <span> { </span><span>189</span> <span> ContentDecorator::end(); </span><span>190</span> <span> } </span><span>191</span> <span>192</span> <span>/*</span><span>* </span><span>193</span> <span> * Begins fragment caching. 開始片段緩存 </span><span>194</span> <span> * This method will display cached content if it is available. </span><span>195</span> <span> * If not, it will start caching and would expect an [[endCache()]] </span><span>196</span> <span> * call to end the cache and save the content into cache. </span><span>197</span> <span> * 展示可用的緩存內容,否則將開始緩存內容直到出現(xiàn)[endCache()]方法 </span><span>198</span> <span> * A typical usage of fragment caching is as follows, </span><span>199</span> <span> * </span><span>200</span> <span> * ~~~ </span><span>201</span> <span> * if ($this->beginCache($id)) { </span><span>202</span> <span> * // ...generate content here </span><span>203</span> <span> * $this->endCache(); </span><span>204</span> <span> * } </span><span>205</span> <span> * ~~~ </span><span>206</span> <span> * </span><span>207</span> <span> * @param string $id a unique ID identifying the fragment to be cached.緩存片段的唯一標識 </span><span>208</span> <span> * @param array $properties initial property values for [[FragmentCache]]初始屬性[FragmentCache] </span><span>209</span> <span> * @return boolean whether you should generate the content for caching. 是否生成緩存的內容。 </span><span>210</span> <span> * False if the cached version is available. </span><span>211</span> <span>*/</span> <span>212</span> <span>public</span> function beginCache($id, $properties =<span> []) </span><span>213</span> <span> { </span><span>214</span> $properties[<span>'</span><span>id</span><span>'</span>] = $id; <span>//</span><span>片段標識</span> <span>215</span> $properties[<span>'</span><span>view</span><span>'</span>] = $<span>this</span>; <span>//</span><span>調用初始化屬性</span> <span>216</span> <span>/*</span><span> @var $cache FragmentCache </span><span>*/</span> <span>217</span> $cache =<span> FragmentCache::begin($properties); </span><span>218</span> <span>if</span> ($cache->getCachedContent() !== <span>false</span><span>) { </span><span>219</span> $<span>this</span>->endCache();<span>//</span><span>從緩存中讀取到了緩存的內容,則渲染內容并返回 false,不再進行緩存</span> <span>220</span> <span>221</span> <span>return</span> <span>false</span><span>; </span><span>222</span> } <span>else</span><span> { </span><span>223</span> <span>return</span> <span>true</span><span>; </span><span>224</span> <span> } </span><span>225</span> <span> } </span><span>226</span> <span>227</span> <span>/*</span><span>* </span><span>228</span> <span> * Ends fragment caching. 結束片段緩存 </span><span>229</span> <span>*/</span> <span>230</span> <span>public</span><span> function endCache() </span><span>231</span> <span> { </span><span>232</span> <span> FragmentCache::end(); </span><span>233</span> <span> } </span><span>234</span> <span>235</span> <span>/*</span><span>* </span><span>236</span> <span> * Marks the beginning of a page.頁面開始標記 </span><span>237</span> <span>*/</span> <span>238</span> <span>public</span><span> function beginPage() </span><span>239</span> <span> { </span><span>240</span> ob_start(); <span>//</span><span>打開輸出緩沖</span> <span>241</span> ob_implicit_flush(<span>false</span>);<span>//</span><span>關閉緩沖區(qū)</span> <span>242</span> <span>243</span> $<span>this</span>-><span>trigger(self::EVENT_BEGIN_PAGE); </span><span>244</span> <span> } </span><span>245</span> <span>246</span> <span>/*</span><span>* </span><span>247</span> <span> * Marks the ending of a page. 頁面結束標記 </span><span>248</span> <span>*/</span> <span>249</span> <span>public</span><span> function endPage() </span><span>250</span> <span> { </span><span>251</span> $<span>this</span>-><span>trigger(self::EVENT_END_PAGE); </span><span>252</span> ob_end_flush();<span>//</span><span>關閉輸出緩沖區(qū)</span> <span>253</span> }

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

How to display the source code of PHP code in the browser without being interpreted and executed? PHP is a server-side scripting language commonly used to develop dynamic web pages. When a PHP file is requested on the server, the server interprets and executes the PHP code in it and sends the final HTML content to the browser for display. However, sometimes we want to display the source code of the PHP file directly in the browser instead of being executed. This article will introduce how to display the source code of PHP code in the browser without being interpreted and executed. In PHP, you can use

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not
