3

我正在使用 Kohana 3,它是模板控制器。我的主站点模板控制器目前看起来像这样:

<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_SiteTemplate extends Controller_Template
{
    public function before()
    {
        parent::before();

        // Initialize default template variables
        $this->template->styles           = Kohana::config('site.styles');
        $this->template->scripts          = Kohana::config('site.scripts');

        $this->template->title            = '';
        $this->template->content          = '';
    }
}

然后在我的模板视图中我这样做:

<?php # Styles
foreach($styles as $file => $media) 
    echo HTML::style($file, array('media' => $media)).PHP_EOL ?>

<?php # Scripts
foreach($scripts as $file) 
    echo HTML::script($file).PHP_EOL ?>

这工作正常。问题是它需要将样式和脚本文件添加到控制器中,而实际上不必关心这些。如果视图是由我以外的其他人完成的,这也很麻烦,因为他们不得不在控制器上胡闹,只是为了添加一个新的样式表或一个新的脚本文件。如何以更好的方式做到这一点?

只是为了澄清,我想知道的是如何处理特定于页面的样式表和脚本。默认和站点范围的我从配置文件中获取或直接放在模板视图中没有问题。我的问题是如何以一种好的方式为特定页面添加自定义页面。

4

4 回答 4

1

Controller_Template 实际上放置定期加载的样式和 javascript的逻辑的地方,这就是它被称为 Controller_Template 的原因。

您应该通过扩展 Controller_Template 的控制器来添加/编辑它们。最后,每个使用 Kohana 的开发人员都应该首先了解这些东西是如何工作的。

关于内联脚本/样式,只需将它们放在视图文件中,它们是内联的,对吗?

于 2010-05-14T21:08:01.963 回答
0
    parent::before();

    // Initialize default template variables
    $this->template->styles           = Kohana::config('site.styles');
    $this->template->scripts          = Kohana::config('site.scripts');

    $this->template->title            = '';
    $this->template->content          = '';

    /* make your view template available to all your other views so easily you could
       access template variable
    */
    View::bind_global('template', $this->template);

在某个视图中使用这样的东西

   $template->scripts[] = 'js/example.js';

并在最后的主视图(模板视图)中,您将拥有来自不同视图的集合 js 数组

  <?php
   foreach($template->scripts as $file) { echo Html::script($file), "\n"; }
  ?>

使用这种方法,您必须在使用的每个视图上调用渲染方法(在模板视图脚本之上执行此操作),然后插入带有渲染内容的变量,如下所示

$top = View::factory('elements/top_nav')->render();
$content = $content->render();
$footer = View::factory('elements/footer')->render();

<div id="content"> 
        <?php echo $content;?>
    </div>
    <div id="meniu"><?php echo $top;?></div> you could add inline scripts as well, just add a $template->inline_scripts populate it with $template->inline_scripts = array('<script type="text/javascript">Cufon.replace(\'#meniu ul.topnav li a\');</script>'); and show it on template view script with a foreach($template->inline_scripts as $inline_script) { echo $inline_script."\n";};

`

于 2010-10-01T20:31:00.773 回答
0

你为什么不把它们放在视图中?- 那么如果其他人正在处理视图,他们可以看到正在使用的样式表并控制它们而不接触控制器等?

例如:

<?php echo HTML::style("/css/style.css", array('media' => 'screen')).PHP_EOL ?>

伊恩

于 2010-05-14T23:37:08.427 回答
0

一种方法是创建一个帮助程序来存储 CSS 和脚本文件的列表。这个助手将作为一个单例工作。然后,在适当的时候,您可以将这些链接转储到您的模板中。

IE:

帮助代码:

<?php defined('SYSPATH') or die('No direct script access.');

class javascript {
    private static $files = array();

    public static function add($file)
    {
        self::$files[] = $file;
    }

    public static function get()
    {
        return self::$files;
    }
}

控制器代码:

class Controller_Example extends Controller_Template {

    public function before()
    {
        ....
        javascript::add('js/jquery.js');
        javascript::add('js/jquery-ui.js');
        ....
    }
}

模板代码:

<html>
    <head>
        <?php foreach(javascript::get() as $javascript ?>
        <script type="text/javascript" src="<?= $javascript ?>"></script> 
        <?php endforeach; ?>
    </head>
...
</html>

需要考虑的几个改进:

  • 创建一个基类作为 CSS 版本的模板。
  • 添加一个方法来添加包含在正文中的脚本文件,这是 FBJS 所必需的。

PS:这是曾经在 IRC 上推荐过的 Kohana 项目的主要策略之一。

于 2010-05-14T20:46:44.360 回答