我需要3个不同的模板为我的Codeigniter应用程序。我读过关于主题库的文章。但是我仍然不知道如何将模板添加到Codeignier中。
我了解了如何在Controller中调用模板。
请帮帮忙
发布于 2012-03-09 10:08:11
我正在使用这个模板库,它非常简单,对我来说效果很好。
应用程序/库/Template.php
<?php
class Template {
var $template_data = array();
var $use_template = '';
/**
* Set variable for using in the template
*/
function set($name, $value)
{
$this->template_data[$name] = $value;
}
/**
* Set template name
*/
function set_template($name)
{
$this->use_template = $name;
}
/**
* Load view
*/
function load($view = '' , $view_data = array(), $template = '', $return = FALSE)
{
$this->CI =& get_instance();
if (empty($template)) {
$template = $this->CI->config->item('template_master');
}
if (!empty($this->use_template)) {
$template = $this->use_template;
}
$this->set($this->CI->config->item('data_container'), $this->CI->load->view($view, array_merge($view_data, array ('template' => $this->template_data)), true));
return $this->CI->load->view($this->CI->config->item('template_folder') . '/' . $template, $this->template_data, $return);
}
}application/config/template.php
<?php
$config['template_master'] = 'main';
$config['template_folder'] = 'templates';
$config['data_container'] = 'content';应用程序/视图/模板/main.php
Header<br />
<?php echo $content; ?></br>
Footer应用程序/控制器/欢迎.php
<?php
class Welcome extends CI_Controller
{
public function index()
{
$this->load->config('template');
$this->load->library('template');
$this->template->load('welcome', array('view' => 'data'));
}
}我通常将配置/库文件放在自动加载上,您可以随时使用$this->template->set_template('other_template');来使用其他文件:)
希望能有所帮助。
发布于 2012-03-09 10:07:52
我在一个CodeIgniter项目中使用了以下设置:
不同的模板以及样式表和图像位于以下文件夹中:
/templates/1/header.php
/templates/1/footer.php
/templates/1/images/*
/templates/1/style/*
/templates/2/header.php
/templates/2/footer.php
/templates/2/images/*
/templates/2/style/*在控制器中,确定要加载的模板,并将该模板的路径作为变量(在本例中为templatepath )传递给视图文件。在视图文件中,您可以执行以下操作:
<?php include($templatepath.'/header.php'); ?>在顶端和
<?php include($templatepath.'/footer.php'); ?>在底部。
https://stackoverflow.com/questions/9627947
复制相似问题