如何管理多个模板和模板资产

时间:2022-10-13 12:31:21

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

我是Kohana的新手,并且一直在阅读文档,教程和论坛帖子,以了解它是如何工作的。

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.

我试图在我的一个应用程序上实现这个框架,现在我被困在管理多个模板和它的资产。

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.

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

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

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

2 个解决方案

#1


4  

In short Yes

总之是的

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.

我在使用Kohana 3.0制作的网站上自己使用模板。我将尝试解释它的基本设置;使用模板你的控制器需要扩展Controller_Template,里面的$ template变量指定要在views文件夹中加载的模板页面,所以我创建了自己的主控制器类,扩展了controller_template类来管理要加载的模板;在下面你会看到我的默认模板的名称只是模板,所以如果我的控制器上没有指定,我会从我的views文件夹中加载template.php。

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

我有一个master.php主控制器,类定义为(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;
        }
    }
}

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

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

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

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

现在假设我们有一个需要使用不同模板的控制器,所以假设你有一个带有类定义的photo.php照片控制器

// 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 contains something like

/views/template.php包含类似的内容

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

/views/templat2e.php contains a different layout like

/views/templat2e.php包含不同的布局

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

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.

其中$ header,$ sidebar,$ content和$ footer在主控制器中设置或由控制器中的代码用$ this-> template-> header等覆盖。

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

希望这足以解释如何在Kohana中使用模板。

#2


2  

I'd advise against structuring your assets this way because you won't be using the framework as it was intended.

我建议不要以这种方式构建资产,因为你不会像预期的那样使用框架。

CSS, JavaScript and Images should really be stored outside of the main application directory

CSS,JavaScript和图像应该存储在主应用程序目录之外

e.g.

例如

application/
modules/
system/
assets/
  template1/
    css/
      styles.css
    js/
      script.js
    img/
      thing.jpg
  template2/
    css/
      styles.css
    js/
      script.js
    img/
      another-thing.jpg

Otherwise every asset request is going to be routed through the framework which is going to impact on the responsiveness of your application.

否则,每个资产请求都将通过框架进行路由,这将影响应用程序的响应能力。

If you still want to go down this path have a look at the action_media method in /modules/userguide/classes/controller/userguide.php to see how CSS, JavaScript and Images are loaded by the userguide module.

如果您仍想沿着这条路走下去,请查看/modules/userguide/classes/controller/userguide.php中的action_media方法,了解用户指南模块如何加载CSS,JavaScript和图像。

Alternatively you could amend your .htaccess rules to allow access to asset files beneath the application directory.

或者,您可以修改.htaccess规则以允许访问应用程序目录下的资产文件。

#1


4  

In short Yes

总之是的

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.

我在使用Kohana 3.0制作的网站上自己使用模板。我将尝试解释它的基本设置;使用模板你的控制器需要扩展Controller_Template,里面的$ template变量指定要在views文件夹中加载的模板页面,所以我创建了自己的主控制器类,扩展了controller_template类来管理要加载的模板;在下面你会看到我的默认模板的名称只是模板,所以如果我的控制器上没有指定,我会从我的views文件夹中加载template.php。

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

我有一个master.php主控制器,类定义为(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;
        }
    }
}

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

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

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

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

现在假设我们有一个需要使用不同模板的控制器,所以假设你有一个带有类定义的photo.php照片控制器

// 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 contains something like

/views/template.php包含类似的内容

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

/views/templat2e.php contains a different layout like

/views/templat2e.php包含不同的布局

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

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.

其中$ header,$ sidebar,$ content和$ footer在主控制器中设置或由控制器中的代码用$ this-> template-> header等覆盖。

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

希望这足以解释如何在Kohana中使用模板。

#2


2  

I'd advise against structuring your assets this way because you won't be using the framework as it was intended.

我建议不要以这种方式构建资产,因为你不会像预期的那样使用框架。

CSS, JavaScript and Images should really be stored outside of the main application directory

CSS,JavaScript和图像应该存储在主应用程序目录之外

e.g.

例如

application/
modules/
system/
assets/
  template1/
    css/
      styles.css
    js/
      script.js
    img/
      thing.jpg
  template2/
    css/
      styles.css
    js/
      script.js
    img/
      another-thing.jpg

Otherwise every asset request is going to be routed through the framework which is going to impact on the responsiveness of your application.

否则,每个资产请求都将通过框架进行路由,这将影响应用程序的响应能力。

If you still want to go down this path have a look at the action_media method in /modules/userguide/classes/controller/userguide.php to see how CSS, JavaScript and Images are loaded by the userguide module.

如果您仍想沿着这条路走下去,请查看/modules/userguide/classes/controller/userguide.php中的action_media方法,了解用户指南模块如何加载CSS,JavaScript和图像。

Alternatively you could amend your .htaccess rules to allow access to asset files beneath the application directory.

或者,您可以修改.htaccess规则以允许访问应用程序目录下的资产文件。