【问题标题】:how to manage multiple templates and template assets如何管理多个模板和模板资产
【发布时间】:2011-12-30 18:38:15
【问题描述】:

我是 Kohana 的新手,一直在阅读文档、教程和论坛帖子以了解其工作原理。

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

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

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

【问题讨论】:

    标签: kohana kohana-3 kohana-3.2


    【解决方案1】:

    我建议不要以这种方式构建您的资产,因为您不会按照预期使用框架。

    CSS、JavaScript 和图像确实应该存储在主 application 目录之外

    例如

    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
    

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

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

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

    【讨论】:

      【解决方案2】:

      总之是的

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

      我有一个 ma​​ster.php 主控制器,其类定义为(已简化)

      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 用户控制器,其类定义为

      // 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 照片控制器,其类定义为

      // 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 包含不同的布局,例如

          <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-&gt;template-&gt;header 等控制器中的代码覆盖。

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

      【讨论】:

      • 为什么要在你的master.php控制器中引入一个局部变量$template?似乎没有必要。 // Set a local template variable to what template the controller wants to use, by default 'template' $template = $this-&gt;template;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      相关资源
      最近更新 更多