CodeIgniter 公共标头包含
我对 CodeIgniter 很陌生,但我想我已经掌握了它的工作原理。
通常,当我制作一个网站(例如 5 页网站)时,我的头文件包含在每个页面中包含的 php 文件中,这样,如果我必须更改标头,我只需在一个位置进行操作,而不是在其中进行更改。改变它五次。
在我的 CodeIgniter 应用程序中,我的控制器中的每个页面都有一个函数,它根据函数加载不同的视图。例如,
public function Index() {
$data = array();
$this->load->view('index',$data);
}
public function blog() {
$data = array();
$this->load->view('inner1',$data);
}
然后我可以将所有逻辑放入控制器中。
拥有一个引用标头的最佳方式是什么?我应该将它作为变量放入控制器中,然后将其作为数据发送到每个视图吗?
另外,如果有更有效的方法,请推荐!
谢谢!
I am very new to CodeIgniter, but I think I have a grasp on how it works.
Typically when I make a website, like a 5-page site, I have my header file in a php file which is included in each page, so that if I have to alter the header I only have to do it in one spot rather than changing it five times.
In my CodeIgniter application, I have a function for each page in my controller which loads a different view, depending on the function. For example,
public function Index() {
$data = array();
$this->load->view('index',$data);
}
public function blog() {
$data = array();
$this->load->view('inner1',$data);
}
Then I can put all my logic in the controller.
What's the best way to have one referenced header? Should I put it in the controller as a variable and then send it as data to each view?
Also, if there's a more efficient way of doing this, please suggest it!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常做的是在视图中创建标题,然后将标题视图添加到索引上方的控制器...您也可以对页脚执行相同的操作。
所以它会是这样的:
有道理吗?
添加:您还可以在其中包含整个头标签,例如元标签、标题、CSS 链接等。但我通常将它们放在另一个视图中,因为有时它们根据页面的不同而有所不同。
What I usually do is make the header in a view, and then add the header view to the controller above index... you can do the same with footer too.
So it would be something like:
Make sense?
Added: You can also include your entire head tag in there too, like your meta tags, title, css links etc. But I usually put those in another view because sometimes they are different depending on the page.
我经常使用 CodeIgniter。我的 /views 文件夹中有一个名为 /common 的文件夹。在那里,我放置了 footer.php、sidebar.php、header.php 等。
我的视图文件如下所示:
我的控制器文件如下所示:
请注意,变量 $title 或 $meta 稍后可以在标头模板中调用。这样,我可以对 common/header.php 进行更改并在整个网站上保留。
祝你好运!
PD:我建议使用登山扣来管理每个控制器顶部的资产,然后将它们显示在 header.php 上
I use CodeIgniter a lot. I have a folder named /common inside my /views folder. There, I put my footer.php, sidebar.php, header.php, and so.
My view files looks like:
My controller files looks like:
Make note that the variables as $title or $meta, can be later called within the header template. This way, I can make changes on common/header.php and the persist across the site.
Good luck!
PD: I recommend using Carabiner for managing your assets at the top of every controller and then display them on your header.php
您看过任何模板项目吗?
http://williamsconcepts.com/ci/codeigniter/libraries/template/
http://philsturgeon.co.uk/index.php/code/codeigniter-template
这些将允许您创建一个主模板(或更多,如果您需要的话),您可以在单个文件中进行站点范围的更改。这些更改可能包括标题,但也可能包括其他区域。
Have you looked at any of the template projects out there?
http://williamsconcepts.com/ci/codeigniter/libraries/template/
http://philsturgeon.co.uk/index.php/code/codeigniter-template
These would allow you to create a master template (or more if you needed) where you could make site-wide changes in a single file. Those changes could include the header, but they could include other regions as well.