codeigniter如何加载视图文件并传递变量?
我已经阅读了源代码,但它似乎有点神秘。我只是想了解 CI 如何将数组转换为视图可用的单个变量。
我认为该视图包含在 include() 中,但变量似乎只对视图有效。
控制器:
$this->load->view('about', array('title' => 'about'));
视图:
<?php echo $title; // shows 'about' ?>
I've read through the source code but it seems a little cryptic. I'm just trying to get my head around how CI transforms an array into individual variables available to the view.
I gather that the view is included with include(), but the variables seem to only be effective for the view.
Controller:
$this->load->view('about', array('title' => 'about'));
View:
<?php echo $title; // shows 'about' ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
php extract() 函数
变量 $test 和 $key 将为“仅当它们在本地声明时才在视图中可见”,所以假设一个函数包含视图文件,并且在包含它之前,它将提取值,然后变量仅在该函数内部可见(女巫主体将包含视图文件),这并不是 CI 真正的做法,但它解释了原理。
php extract() function
The variables $test and $key would be "visible" in the view only if they are declared localy, so let's say a function includes the view file and right before including it, it will extract the values, then the variables would be visible only inside that function ( witch body would contain the view file too ), it's not realy how CI does it but it explains the principle .