2012-03-09 80 views
1

我需要3个不同的模板用于我的Codeigniter应用程序。我读过关于主题的图书馆。但我仍然没有得到任何关于如何添加模板到Codeignier的想法..代码点燃器主题库。

我了解如何在Controller中包含模板。

请帮

+1

从控制器:'$ this-> load-> view('view_name');' - 更多信息在http://codeigniter.com/user_guide/general/views.html – 2012-03-09 02:00:35

回答

2

我使用这个模板库,非常简单,适用于我。

应用/库/的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); 
    } 
} 

应用/配置/的template.php

<?php 
$config['template_master'] = 'main'; 
$config['template_folder'] = 'templates'; 
$config['data_container'] = 'content'; 

应用/视图/模板/ main.php

Header<br /> 
<?php echo $content; ?></br> 
Footer 

应用/ controllers/welcome.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');使用另一个:)

希望它有帮助。

+0

感谢您的评论,我是要试试这个..更多疑问...如何在模板文件夹中保存不同的模板?任何特定的命名约定或文件夹结构? – ramesh 2012-03-09 02:18:12

+1

由于图书馆加载模板的方式是使用CI的视图库,因此您可以放置​​任何想要的名称。我习惯于将它们命名为:main.php,admin.php,unauthorized.php等 – 2012-03-09 02:20:59

+0

好的,谢谢...正如你所说我要在库文件中保存这个“Template.php”创建一个名为“ main.php“也与confg变更和所有...之后,对于下一个模板,我需要创建一个名为”movies.php“的新模板并保存在模板文件夹中,并通过$ this-> template-> load将其加载到控制器中('movies',array('view'=>'data')); ......请问这个工作还是我需要再次做任何confg更改...谢谢 – ramesh 2012-03-09 02:31:50

1

我使用以下设置的笨项目:

与样式表和图像沿不同的模板是在以下文件夹:

/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'); ?> 

在底部。