1

我的网站是 www.kipclip.com,它在 Kohana 上运行。我创建了一个新的租赁页面。但它不会占用我的 CSS 和 JS 文件。我试图找出这是如何包含的,或者 Kohana 是否有特殊的方法可以做到这一点。但仍然没有成功。你对此有什么想法吗?

4

1 回答 1

0

一种快速而肮脏的方法是在您使用常规 html 脚本和样式标签实现的视图中添加脚本和样式的名称,然后从那里继续。

但是,如果你不喜欢又快又脏,而喜欢做得更好更具体,如果你使用的是 Kohana 3.2,你可以这样做。我没有在旧版本或新版本上尝试过,所以它可能会或可能不会在它们中工作(如果您尝试将其移植到该版本,请查阅与您希望移植的相关版本相关的过渡文档):

/**
*      /application/classes/controller/application.php
*/
abstract class Controller_Application extends Controller_Template {

    public function before() {
        parent::before();

        if($this->auto_render) {
            //Initialize empty values for use by ALL other derived classes
            $this->template->site_name = '';//this is a psuedo-global set in this class
            $this->template->title = '';//this too is set by the controller and action
            $this->template->content = ''; //this is set by the controller and action
            $this->template->styles = array();
            $this->template->scripts = array();
            $this->template->admin_scripts = array();
        }
    }    

    /**
     * The after() method is called after your controller action.
     * In our template controller we override this method so that we can
     * make any last minute modifications to the template before anything
     * is rendered.
     */
    public function after() 
    {
        if ($this->auto_render) {

            //set the CSS files to include
            $styles = array(
                'style1', //the css file with all the defaults for the site
                'jquery-library-css'
            );


            //set the JavaScript files to include
            $scripts = array(
                'myscript1',
                'myscript2'
            );


            $admin_scripts = array(
                'jquery-admin-functions',
            );
            //now, merge all this information into one so that it can be accessed
            //by all derived classes:
            $this->template->styles = array_merge($this->template->user_styles, $user_styles);
            $this->template->scripts = array_merge($this->template->user_scripts, $user_scripts);
            $this->template->admin_scripts = array_merge($this->template->admin_scripts, $admin_scripts);
        }

        //bind the site_name to the template view
        $this->template->site_name = 'My Site Name';


        //OLD WAY shown below:
        View::set_global('site_name', 'My Site Name'); //set the site name

        //now that everything has been set, use parent::after() to finish setting values
        //and start rendering
        parent::after();
    }

}

那么,这是如何工作的呢?请记住,application.php该类是所有其他控制器类派生自的基本控制器类。通过对基本控制器实现这种类型的绑定,每个派生控制器都可以访问可用的脚本、样式等。因此,该控制器调用的每个关联视图也可以访问这些变量。

因此,现在要在您的视图中访问这些变量:

例如,模板 PHP 文件:/application/views/template.php

如果它是这样定义的(使用 PHP 短标签 - 但不要在生产代码中使用短标签!):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<head>
    <meta charset='utf-8'/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<?php
/**
 * Link the css files stored in the `static` folder from the project root.
 * This will vary depending on how you have your files saved.
 */
foreach($user_styles as $style) : ?>
    <link rel="stylesheet" href="<?php echo URL::base() . 'static/css/' . $style ?>.css" type="text/css"/>
<?php endforeach; ?>
    <?php //Create AND set a dynamic page title - much like Facebook ?>
    <title><?php echo $site_name; if(!empty($title)) echo ' - ' . $title; ?></title>
</head>
<body>

<!-- Fill in the body with HTML, PHP - whatever you want -->

<?php
/**
 * Now, load the scripts:
 * According to Yahoo, for better site performance, all scripts should be loaded after the body has been loaded
 */

foreach($user_scripts as $script) : ?>
    <script src="<?php echo URL::base() . 'static/js/' . $script; ?>.js" type="text/javascript"></script>
<?php endforeach; ?>
</body>
</html>

所有这些有两个要点

一:如果您希望某些东西是全局的,或者对所有控制器(和后续视图)都可用,请定义它们并将它们绑定到基础应用程序控制器类中。

Two: As a result, this functionality also gives you tremendous leverage and power in that if you have derived classes, you can them implement this same type of binding to that particular controller class making it available to any subsequent derived controller classes. That way, if you have two classes that should not have access to certain files and their associated functionality, e.g. an admin JavaScript file that loads all posts by some user, then this kind of implementation can make your life much, much, much easier.

And, a third hidden option is that give Kohana is PHP, you can use regular vanilla PHP / HTML in an associated view if you can't figure it out immediately. Although, that is something that I would dissuade you from doing in production code.

Whichever way, I hope this can assist you.

于 2013-08-10T01:33:09.537 回答