php 如何处理 Codeigniter 模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1029960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Deal With Codeigniter Templates?
提问by Martin
I'm fairly new to MVC, and I've found CodeIgniter recently. I'm still learning everyday, but one problem is its template engine. What is the best way to create templates in CodeIgniter?
我对 MVC 还很陌生,最近我发现了 CodeIgniter。我每天都在学习,但一个问题是它的模板引擎。在 CodeIgniter 中创建模板的最佳方法是什么?
CakePHP comes with its own template library, is there a similar feature in CodeIgniter?
CakePHP 自带模板库,CodeIgniter 有没有类似的功能?
回答by Ryan Schumacher
Unlike other frameworks CodeIgniter does not have a global template system. Each Controller controls it's own output independent of the system and views are FIFO unless otherwise specified.
与其他框架不同,CodeIgniter 没有全局模板系统。每个控制器独立于系统控制它自己的输出,除非另有说明,否则视图是 FIFO。
For instance if we have a global header:
例如,如果我们有一个全局标头:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title><?=$title?></title>
<!-- Javascript -->
<?=$javascript ?>
<!-- Stylesheets -->
<?=$css ?>
</head>
<body>
<div id="header">
<!-- Logos, menus, etc... -->
</div>
<div id="content">
and a global footer:
和一个全局页脚:
</div>
<div id="footer">
<!-- Copyright, sitemap, links, etc... -->
</div>
</body>
</html>
then our controller would have to look like
那么我们的控制器必须看起来像
<?php
class Welcome extends Controller {
function index() {
$data['title'] = 'My title';
// Javascript, CSS, etc...
$this->load->view('header', $data);
$data = array();
// Content view data
$this->load->view('my_content_view', $data);
$data = array();
// Copyright, sitemap, links, etc...
$this->load->view('footer', $data);
}
}
There are other combinations, but better solutions exist through user libraries like:
还有其他组合,但通过用户库存在更好的解决方案,例如:
See Comments Below
请参阅下面的评论
回答by jruzafa
I've tried several ways to do codeigniter templates and the way that I stay is the fastest and simplest, is as follows.
我已经尝试了几种方法来做 codeigniter 模板,我留下的方式是最快和最简单的,如下所示。
In controller:
在控制器中:
//Charge the view inside array
$data['body'] = $this->load->view('pages/contact', '', true);
//charge the view "contact" in the other view template
$this->load->view('template', $data);
In view template.php:
在视图 template.php 中:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
<head>
<title>Template codeigniter</title>
</head>
<body>
<div>
<?=$body?>
</div>
<div class="clear"></div>
<div>Footer</div>
</div>
</body>
</html>
$bodyis the view contact.
$body是视图联系人。
回答by SpartakusMd
Make a library that includes all your views and send it data that you need to send to your content view. This is all!
制作一个包含所有视图的库,并将您需要发送到内容视图的数据发送给它。这就是全部!
<?php
class Display_lib
{
public function user_page($data,$name)
{
$CI =& get_instance ();
$CI->load->view('preheader_view',$data);
$CI->load->view('header_view');
$CI->load->view('top_navigation_view');
$CI->load->view($name.'_view',$data);
$CI->load->view('leftblock_view',$data);
$CI->load->view('rightblock_view',$data);
$CI->load->view('footer_view');
}
}
回答by Jerome Jaglale
This library, easy to use and customize, does exactly what you'd expect:
这个库易于使用和定制,完全符合您的期望:
- avoid HTML duplication (header, footer..)
- no need to learn a new language (!)
- 避免 HTML 重复(页眉、页脚......)
- 无需学习一门新语言(!)
回答by GloryFish
I use CodeIgniter with Smarty and it's great (if you like Smarty, I do).
我将 CodeIgniter 与 Smarty 一起使用,它很棒(如果你喜欢 Smarty,我喜欢)。
Say you have an article controller, you could do somehting like this in it:
假设您有一个文章控制器,您可以在其中执行以下操作:
class Article extends Controller {
function show_all() {
$articles = $this->article_model->get_all();
$this->smarty->assign('entities', $articles);
$this->smarty->view('list');
}
}
And then in your template:
然后在您的模板中:
{include file="header.tpl"}
<ul>
{foreach from=$entities item=entity}
<li>{$entity.title}</li>
{/foreach}
</ul>
{include file="footer.tpl"}
The nice part about this is that the controller doesn't really need to know about headers and footers. It just knows that a group of articles should be shown as a list. From there, it's just the templates that are responsible for defining how a list of things are displayed, in this case, in a ul between a header and footer.
这样做的好处是控制器并不真正需要知道页眉和页脚。它只知道应该将一组文章显示为列表。从那里,它只是负责定义如何显示事物列表的模板,在这种情况下,在页眉和页脚之间的 ul 中。
Another cool thing you can do is use this list template for things that aren't articles. You could have a list of users or pages or whatever. In some cases reusing a template like this can be useful. Not always, but sometimes.
您可以做的另一件很酷的事情是将此列表模板用于非文章的内容。您可以拥有用户或页面或其他任何内容的列表。在某些情况下,重复使用这样的模板可能很有用。不总是,但有时。
Setting up CodeIgniter for smarty is pretty straightforward. It's a matter of copying the Smarty files to your library folder and creating a simple wrapper for it. You can find instructions here:
为 smarty 设置 CodeIgniter 非常简单。只需将 Smarty 文件复制到您的库文件夹并为其创建一个简单的包装器即可。您可以在此处找到说明:
http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html
http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html
Once you get is set up it's great.
一旦你得到设置它是伟大的。
回答by Robert
I have two primary templates; one for the site and one for our admin panel. Here is my setup for our main site (mostly static)... I decided on one controller called siteIt calls the template file and each page and gets itsview file.
我有两个主要模板;一份用于网站,一份用于我们的管理面板。这是我的主要站点的设置(大部分是静态的)...我决定使用一个名为site它的控制器,它调用模板文件和每个页面并获取其视图文件。
Why doesn't anyone mention template engine use? Are -just- views better/faster?
为什么没有人提到模板引擎的使用?-just- 视图更好/更快吗?
In
config/template.phpI defined the template(s). Note *site_template* is in theviewsfolder:$template['site']['template'] = 'site_template'; $template['site']['regions'] = array('title','section','col2','content',); $template['site']['parser'] = 'parser'; $template['site']['parser_method'] = 'parse'; $template['site']['parse_template'] = FALSE;In
config/routers.phpI setup rules to handle the requests for thesitecontroller which are single segments urls mostly but we do have one sectionthat is structured as such; /who-we-areand then for selected people /who-we-are/robert-wayneand so:$route['what-we-do'] = 'site/what_we_do'; $route['who-we-are'] = 'site/who_we_are'; $route['who-we-are/(:any)'] = "site/who_we_are/"And
controllers/site.phpAgain with a function for each page/section:class Site extends CI_Controller { function __construct() { parent::__construct(); $this->template->set_template('site'); // ask for the site template $this->load->library('mobile'); } public function index() { $data = array('section' => 'home'); $this->template->write_view('col2', 'site/menu.php', $data); $this->template->write('title', "COOL PAGE TITLE", TRUE); $this->template->write('section', $data['section'], TRUE); $this->template->write_view('content', 'site/welcome', $data); $this->template->render(); } public function who_we_are() { // this bit readies the second segment. $slug = str_replace('-', '_', $this->uri->segment(2, 0)); if($slug) // IF there is a second segment we load the person. { $data['bio'] = $this->load->view('site/people/'.$slug, '', true) } else { // where it loads the general view who_we_are } // and so on for each page...
在
config/template.php我定义了模板。注意 *site_template* 在views文件夹中:$template['site']['template'] = 'site_template'; $template['site']['regions'] = array('title','section','col2','content',); $template['site']['parser'] = 'parser'; $template['site']['parser_method'] = 'parse'; $template['site']['parse_template'] = FALSE;在
config/routers.php我设置规则来处理对site控制器的请求,这些请求 主要是单段 url,但我们确实有一个这样结构的部分;/who-we-are然后对于选定的人/who-we-are/robert-wayne等等:$route['what-we-do'] = 'site/what_we_do'; $route['who-we-are'] = 'site/who_we_are'; $route['who-we-are/(:any)'] = "site/who_we_are/"并
controllers/site.php再次对每一页/部分功能:class Site extends CI_Controller { function __construct() { parent::__construct(); $this->template->set_template('site'); // ask for the site template $this->load->library('mobile'); } public function index() { $data = array('section' => 'home'); $this->template->write_view('col2', 'site/menu.php', $data); $this->template->write('title', "COOL PAGE TITLE", TRUE); $this->template->write('section', $data['section'], TRUE); $this->template->write_view('content', 'site/welcome', $data); $this->template->render(); } public function who_we_are() { // this bit readies the second segment. $slug = str_replace('-', '_', $this->uri->segment(2, 0)); if($slug) // IF there is a second segment we load the person. { $data['bio'] = $this->load->view('site/people/'.$slug, '', true) } else { // where it loads the general view who_we_are } // and so on for each page...
and as fine point notice the router lets us leave out `/site/' in the url, http://the site.com/who-we-are
并且注意路由器让我们在 url 中省略“/site/”, http://the site.com/who-we-are
thoughts? anyone? bueller?
想法?任何人?布勒?
回答by John Skoumbourdis
Well you can actually use a codeigniter library for templates. The most famous ones are:
好吧,您实际上可以将 codeigniter 库用于模板。最著名的有:
- Codeigniter Simplicity(Actively Developed)
- Phil Sturgeon's Template Library(Not actively developed)
- An Introduction to Views & Templating in CodeIgniter(Here you actually create a template library from scratch)
- Codeigniter 简单性(积极开发)
- Phil Sturgeon 的模板库(未积极开发)
- CodeIgniter 中的视图和模板简介(这里实际上是从头开始创建模板库)
回答by Randell
I'm biased towards this template library made by Carmelo Capinpin because it is so easy to use: link text. Just copy the file in your library and you're ready to go. Instructions on how to use it is in the link I provided.
我偏向于 Carmelo Capinpin 制作的这个模板库,因为它非常易于使用:链接文本。只需复制库中的文件即可开始使用。关于如何使用它的说明在我提供的链接中。
回答by VangelisB
There is a library which allows you to use templates in CodeIgniterin a native style. To load a template/theme just do:
有一个库允许您以原生样式在 CodeIgniter中使用模板。要加载模板/主题,只需执行以下操作:
$this->load->theme(‘theme_name');
To load CSS and javascript files from your views you can do:
要从您的视图加载 CSS 和 javascript 文件,您可以执行以下操作:
$this->load->css(‘path/file.css');
$this->load->js(‘path/file.js');
You can optionally control the way browsers cache CSS & JS files.
您可以选择控制浏览器缓存 CSS 和 JS 文件的方式。
回答by pix0r
A Codeigniter template is generally just a PHP file. You can use all the usual PHP syntax to output variables, do loops, and call other PHP code.
Codeigniter 模板通常只是一个 PHP 文件。您可以使用所有常用的 PHP 语法来输出变量、执行循环和调用其他 PHP 代码。
Sample controller:
示例控制器:
<?php
class Blog extends Controller {
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
Sample view:
示例视图:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
Read more in the docs here: CodeIgniter User Guide: Views
在此处的文档中阅读更多内容:CodeIgniter 用户指南:视图

