how to manage multiple templates and template assets

微笑、不失礼 提交于 2019-12-09 13:51:39

问题


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.

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?


回答1:


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.

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;
        }
    }
}

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

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

    <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

    <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.

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




回答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

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.

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



来源:https://stackoverflow.com/questions/8158017/how-to-manage-multiple-templates-and-template-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!