首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码触发器主题库。

代码触发器主题库。
EN

Stack Overflow用户
提问于 2012-03-09 09:59:17
回答 2查看 2.9K关注 0票数 1

我需要3个不同的模板为我的Codeigniter应用程序。我读过关于主题库的文章。但是我仍然不知道如何将模板添加到Codeignier中。

我了解了如何在Controller中调用模板。

请帮帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-09 10:08:11

我正在使用这个模板库,它非常简单,对我来说效果很好。

应用程序/库/Template.php

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?php
$config['template_master']  = 'main';
$config['template_folder']  = 'templates';
$config['data_container']   = 'content';

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

代码语言:javascript
复制
Header<br />
<?php echo $content; ?></br>
Footer

应用程序/控制器/欢迎.php

代码语言:javascript
复制
<?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');来使用其他文件:)

希望能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2012-03-09 10:07:52

我在一个CodeIgniter项目中使用了以下设置:

不同的模板以及样式表和图像位于以下文件夹中:

代码语言:javascript
复制
/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 )传递给视图文件。在视图文件中,您可以执行以下操作:

代码语言:javascript
复制
<?php include($templatepath.'/header.php'); ?>

在顶端和

代码语言:javascript
复制
<?php include($templatepath.'/footer.php'); ?>

在底部。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9627947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档