在 Codeigniter 的库中加载视图页面
在我的 codeigniter 中,我在库文件夹中创建了一个库。我想加载该库中的视图页面。我该怎么做?
这是我的代码:
$this->load->view('view_page');
但是当我使用此代码时,我收到错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_theme_lib::$load
Filename: libraries/theme_lib.php
Line Number: 9
我的代码中有什么问题?
在库的第 9 行中,代码是:
$this->load->view('view_page');
In my codeigniter i created a library in library folder.I want to load view pages in that library.How can i do this?
This is my code:
$this->load->view('view_page');
But when iam using this code i get an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_theme_lib::$load
Filename: libraries/theme_lib.php
Line Number: 9
What is the problem in mycode?
In Line number 9 in library the code is :
$this->load->view('view_page');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完成您想要做的事情,您需要获取一个 CI 实例并使用它。
例如,
在您的库函数中,您可以使用此变量来加载视图:
我会质疑为什么您要在库中以您已完成的形式调用视图。我怀疑你最好让视图调用返回数据(设置第三个参数“true”),然后让你的库将显示数据返回到控制器......你的方法看起来很混乱,但我没有知道您的图书馆正在尝试做什么......
To do what you're trying to do you need to get an instance of CI, and use that.
e.g.
Within your library functions you could then use this variable to load the view:
I would question though why you want to call a view, in the form that you have done, within a library. I suspect that you would be better to get the view call to return data (setting the 3rd parameter 'true') and then have your library return the display data to the controller.... Your approach seems messy, but then I have no idea what your library is trying to do.....
我出于不同的原因遇到了你的问题,我似乎在将变量传递给视图时遇到问题。在告诉您问题的答案之前,让我先解释一下。
想象一下,您有一个 Emailer 库来发送电子邮件,而不是在控制器中对其进行排序。
Emailer 本身使用视图构建电子邮件字符串。我的问题是,当我从像 Emailer::send_mail($data,$template) 这样的控制器进行调用时,它会正确传递变量,但是当我从另一个库中进行调用时,视图无法注册变量。哈哈
,是的,Stéphane Bourzeix 有时确实希望以不同的方式使用视图中的输出,而不仅仅是返回客户端浏览器。
解决方案就在这里。
https://www.codeigniter.com/userguide2/general/views.html
该页面的最后一部分有类似的东西
东西
也应该起作用
,但如果从控制器以外的其他地方执行此操作,您需要的
:似乎列表中的最后一个参数(true)是告诉它不的参数渲染到浏览器但是相反,只需创建带有模板内容的字符串
我知道这有点太晚了,但希望它仍然对某些人有帮助。祝你的代码好运。
托姆赫雷
I have came across your question for different reason, I seem to have problem passing variables to views instead. Let me explain before I tell you answer to your problem.
Imagine you have an Emailer library to send emails rather than sorting that out in controller.
Emailer than within itself builds email string using views. My problem is that when I make my call from controller something like Emailer::send_mail($data,$template) it passes the variables correctly but when I do it from another library the view fails to register the variables. LOL
So yes Stéphane Bourzeix you do sometimes want to use output from view in a different way than just returning to client browser.
The solution is here.
https://www.codeigniter.com/userguide2/general/views.html
the last section of that page has something like
but something like
should work too
in case of doing this from other places than controllers you will need to:
it seems like the last argument in the list (true) is the one that tells it not to render to browser but instead just create string with template content
I know it's a bit too late but hope it still helps to some. Good luck with your code.
tomhre
您只需不要在库中加载页面(也称为视图)即可。
我认为没有必要这样做。
You simply DON'T load pages (aka Views) in a Library.
I don't see any need for doing this.