如何管理多个模板和模板资产 [英] how to manage multiple templates and template assets

查看:79
本文介绍了如何管理多个模板和模板资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Kohana完全是新手,并且一直在阅读文档,教程和论坛帖子以了解其工作原理.

I am totally newbie with Kohana and have been reading Docs, tutorials and forum posts to know how it works.

我正在尝试在自己的一个应用程序中实现此框架,现在我只能管理多个模板及其资产.

I am trying to implement this framework on one of my application and now I am stuck at managing multiple templates and it's assets.

基本上,我的应用程序将有一个模板文件夹,如template1,template2 ....,并且与特定模板相关的所有图像,css,js都需要包含在模板文件夹中.

Basically my application will have a templates folders like template1, template2 .... and all the images, css, js related with particular template needs to contain within the template folder.

那么有可能有这样的实现吗?如果可以,如何将模板特定的资源加载到父模板文件?

So is it possible to have such implementations? If so how can I load template specific assets to the parent template files?

推荐答案

简而言之

我在用Kohana 3.0创建的网站上自己使用模板.我将尝试解释它的基本设置.要使用模板,您的控制器需要扩展Controller_Template,并且内部的$template变量指定要在您的views文件夹中加载哪个模板页面,因此,我制作了自己的主控制器类,该类扩展了controller_template类以管理要加载的模板;在下面,您会看到我的默认模板的名称只是模板,因此如果未在控制器上指定一个模板,它将从我的views文件夹中加载template.php.

I use templates myself on a site I made with Kohana 3.0. I'll try to explain the basic setup of it; to use templates your controllers need to extend Controller_Template and the $template variable inside specifies what template page to load in your views folder, so I made my own master controller class that extends the controller_template class to manage which template to load; below you'll see my default template's name is simply template so it'll load template.php from my views folder if one isn't specified on my controllers.

我有一个 master.php 主控制器,其类定义为(下拉)

I have a master.php master controller with a class definition of (dumbed down)

abstract class Controller_Master extends Controller_Template
{
    public $template = 'template'; // Default template

    public function before()
    {
        // Set a local template variable to what template the controller wants to use, by default 'template'
        $template = $this->template; 

        // This is important and for abstraction, since we're extending a class and its functions we need to make sure we still execute its before(); function
        // This will load the view you need from /views/template.php or /views/template2.php depending on what your controller specifies into $this->template
        parent::before();

        // Check which template our code/controller needs to use
        if ($template == 'template') 
        {
            $this->template->header = View::factory('template/head');  // Loads default header file from our views folder /views/template/head.php
            $this->template->content = View::factory('template/index');  // Loads default index file from our views folder /views/template/index.php
            $this->template->footer = View::factory('template/footer');  // Loads default footer file from our views folder /views/template/footer.php

            return;
        } elseif ($template == 'template2') 
        {
            $this->template->header = View::factory('template2/head');  // Loads default header file from our views folder /views/template2/head.php
            $this->template->sidebar = View::factory('template2/sidebar');  // Loads default sidebar file from our views folder /views/template2/sidebar.php
            $this->template->content = View::factory('template2/index');  // Loads default index file from our views folder /views/template2/index.php
            $this->template->footer = View::factory('template2/footer');  // Loads default footer file from our views folder /views/template2/footer.php

            return;
        }
    }
}

我有一个 user.php 用户控制器,其类定义为

I have a user.php user controller with a class definition of

// This is important, make sure your controllers extend your master controller class
class Controller_User extends Controller_Master
{
    // In this example this user controller just needs to use the default controller 
    // so nothing needs to be changed on it besides extending our Controller_Master

    // Example action inside the user class on how to load different content into your template instead of the default index page.
    function action_login()
    {
        // Load the login view page from /views/template/forms/login.php
        $this->template->content = View::factory('template/forms/login');
    }
}

现在可以说我们有一个需要使用其他模板的控制器,所以可以说 您有一个 photo.php 照片控制器,其类定义为

Now lets say we have a controller that needs to use a different template so lets say you have have a photo.php photo controller with a class definition of

// This is important, make sure your controllers extend your master controller class
class Controller_Photo extends Controller_Master
{
    // Since this controller needs to use a different template we extend the before() function
    // to override the $template variable we created in master to use 'template2'
    function before() 
    {
        $this->template = 'template2';
    }
}

/views/template.php 包含类似

    <body>
        <div id="header">
            <?= $header; ?>
        </div>
        <div id="content">
            <?= $content; ?>
        </div>
        <div id="footer">
            <?= $footer; ?>
        </div>        
    </body>

/views/templat2e.php 包含另一种布局,例如

/views/templat2e.php contains a different layout like

    <body>
        <div id="header">
            <?= $header; ?>
        </div>
        <div id="sidebar">
            <?= $sidebar; ?>
        </div>
        <div id="content">
            <?= $content; ?>
        </div>
        <div id="footer">
            <?= $footer; ?>
        </div>        
    </body>

在主控制器中设置$header$sidebar$content$footer的位置,或在$this->template->header中用控制器中的代码覆盖$this->template->header.

Where's $header, $sidebar, $content, and $footer are set in the master controller or overwriten by the code in your controller by $this->template->header and so forth.

希望这足以说明如何在Kohana中使用模板.

Hopefully that explains enough how to work with templates in Kohana.

这篇关于如何管理多个模板和模板资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆