codeigniter中的布局/模板/主题是什么

时间:2013-08-14 11:49:54

标签: codeigniter

我是codeigniter的新手,我对codeigniter中的布局/模板/主题感到麻烦。
我不知道什么时候应该使用其中一个..
我能做的最好的方法是什么?如果我想建立一个免费的 html / css模板的网站,如

goodnatured
|--img
   |--img01.jpg
|--css
   |--style.css
|--js
   |--jquery.js
|--index.html

任何人都可以告诉我一个教程,建议,...谢谢

4 个答案:

答案 0 :(得分:1)

我只是写了一些额外的库(application/libraries/display_lib.php)来渲染模板和类似的页面块。 像这样:

    class Display_Lib{
       private $_CI;
       private $_template_data;

       public function __construct()
       {
           $this->_CI =& get_instance();
       }

       public function set($key, $value)
       {
           $this->_template_data[$key] = $value;
       }

       public function get($key)
       {
           return $this->_template_data[$key];
       }

       public function get_template_data()
       {
           return $this->_template_data;
       }

       public function display_page($view, $data = array())
       {
           $this->set('content', $this->_CI->load->view($view, $data, TRUE));
           $this->_CI->load->view('templates/main_template', $this->get_template_data());
       }
}

将此库设置为自动加载:

$autoload['libraries'] = array('session', 'database', 'display_lib');

并在控制器中调用它:

class Main extends CI_Controller{
    public function index()
    {
        $some_data = array();
        $this->display_lib->display_page('views/main_view', $some_data);
    }
}

模板示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <base href="<?=base_url();?>">
    <meta charset="utf-8">
    <link rel="icon" href="<?=site_url('img/favicon.ico')?>" type="image/x-icon"/>
    <link rel="stylesheet" href="<?=site_url('css/style.css');?>" type="text/css" media="screen, projection"/>
    <script type="text/javascript" src="<?=site_url('js/jquery-1.10.2.min.js');?>"></script>
    <title>Some page title</title>
</head>
<body>
    <header></header>
    <div class="auth_wrapper">
        <div class="content">
            <?=$content;?>
        </div>
        <div class="buffer"></div>
    </div>
    <footer></footer>
</body>
</html>

application/views/main_view简单的例子:

<div>Come content will be here</div>

此lib允许使用模板并从控制器渲染视图。

答案 1 :(得分:0)

使用自定义主题(Bootstrapped)检查此存储库CodeIgniter 2.1.4。如果你需要它:包括一个Facebook库。

https://github.com/robinwo/codeigniter-themes-facebook

答案 2 :(得分:0)

模板处理页面的布局。您创建一个模板,其中包含您的元,页眉,页脚和正文空间。身体被注入模板,这是内容改变的地方。

这个想法是,大多数网站不会改变,只有身体发生变化。这是模板有用的地方,它们可以节省您的时间并提高一致性。 看到这个模板库,它非常好:http://getsparks.org/packages/template/show

主题与模板相结合,因为模板通常定义布局,主题只是“皮肤”布局。主题包括将进一步修改模板的资产和样式。

干杯

答案 3 :(得分:-2)

制作template.php,header.php,footer.php。下面是template.php,类似地创建页眉和页脚并将它们全部放在views文件夹中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="icon" href="<?=site_url('img/favicon.ico')?>" type="image/x-icon"/>
    <link rel="stylesheet" href="<?=site_url('css/style.css');?>" type="text/css" media="screen, projection"/>
    <script type="text/javascript" src="<?=site_url('js/jquery-1.10.2.min.js');?>"></script>
    <title>Some page title</title>
</head>
<body>
    <header><?=$this->load->view('header');?></header>
    <div class="wrapper">
        <div class="content">
            <?=$main?>
        </div>
    </div>
    <footer><?=$this->load->view('footer');?></footer>
</body>
</html>

控制器功能中:

function index(){
    $data           = array();
    $data['main']   = "home";           #this view is home.php in views folder, the content part of template.php
    $this->load->view('template', $data); #this is the template file being rendered.
}

这是我认为CI中使用模板的最简单方法。