CodeIgniter 公共标头包含

发布于 2024-10-05 16:19:03 字数 551 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哎呦我呸! 2024-10-12 16:19:03

我通常做的是在视图中创建标题,然后将标题视图添加到索引上方的控制器...您也可以对页脚执行相同的操作。

所以它会是这样的:

public function blog() {

   $data = array();
   $this->load->view('Header'); // just the header file
   $this->load->view('inner1',$data); //your main content
}

有道理吗?

添加:您还可以在其中包含整个头标签,例如元标签、标题、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:

public function blog() {

   $data = array();
   $this->load->view('Header'); // just the header file
   $this->load->view('inner1',$data); //your main content
}

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.

断桥再见 2024-10-12 16:19:03

我经常使用 CodeIgniter。我的 /views 文件夹中有一个名为 /common 的文件夹。在那里,我放置了 footer.php、sidebar.php、header.php 等。

我的视图文件如下所示:

$this->load->view('header');
<div id"main">
.....
</div>


$this->load->view('footer');

我的控制器文件如下所示:

public function blog() {
   $this->data['title'] = "Blog";
   $this->data['meta_tags'] = $meta;
   $this->data['entries'] = $entries;

   $this->load->view('blog',$this->data);
}

请注意,变量 $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:

$this->load->view('header');
<div id"main">
.....
</div>


$this->load->view('footer');

My controller files looks like:

public function blog() {
   $this->data['title'] = "Blog";
   $this->data['meta_tags'] = $meta;
   $this->data['entries'] = $entries;

   $this->load->view('blog',$this->data);
}

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

深海夜未眠 2024-10-12 16:19:03

您看过任何模板项目吗?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文