CodeIgniter:使用模板的技巧
刚刚开始使用 Codeigniter(昨天),我想知道人们正在使用哪些模板功能?
是否可以创建一个视图并在需要时加载它?
谢谢,
琼斯
Just started using Codeigniter (yesterday) and am wondering what templating features people are using?
Is it possible to create a view and just load it whenerever necessary?
Thanks,
Jonesy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模板化的想法是创建具有公共标题的共享布局。页脚等,然后只有一个每页更改的“正文”。
在最基本的级别上,您可以在每个视图中包含页眉和页脚,如下所示:
这可能没问题,但开始构建任何实际大小的应用程序,您都会发现问题。
进行模板化的方法有数百万种,但我多年来使用的方法是这个 模板库。它帮助我完成了 20-30 个不同的项目,并且被许多人使用,所以你知道它已经过尝试和测试。
The idea of templating is to create a shared layout with a common header. footer etc and then just have a "body" that changes per-page.
At the most basic level you can just include header and footer inside each of your views like this:
That can be fine but start building an application of any real size and you'll find problems.
There are million ways of doing templating, but the way I have used for years is this Template library. It's seen me through 20-30 projects varying projects and is used by many so you know it's tried and tested.
是的。这是MVC结构的典型行为,而不仅仅是在CI中。您的视图是表示层,应该基本上没有逻辑/处理。
Yes. This is the typical behavior of the MVC structure, not just in CI. Your views are presentation layers that should be mostly devoid of logic/processing.
另一种方法如下。
在您的控制器中,像这样加载您的模板
在您的模板中,您现在拥有
$template_data
数组来填充它[如果需要!]。您现在可以像这样加载特定视图注意:
template/needed.php
应该位于application/views
文件夹中。specified/viewer.php
文件也应该位于您的views
目录中(即该文件的路径应该类似于WEB_ROOT/application/views/specific/ viewer.php
)这样做的好处是,如果需要的话,任何视图文件都可以用作模板。
Another way to do this is the following.
In your controller, load your template like so
In your template, you now have the
$template_data
array to populate it [if need be!]. You may now load the specific view like soNote:
template/needed.php
should be in theapplication/views
folder.specific/viewer.php
file should also be in yourviews
directory (i.e. the path to this file should be something likeWEB_ROOT/application/views/specific/viewer.php
)The beauty of this is that any view file could be used as a template if need be.