【问题标题】:Why does Kohana doesn't read my CSS?为什么 Kohana 不读取我的 CSS?
【发布时间】:2013-08-09 19:09:06
【问题描述】:

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

【问题讨论】:

    标签: css kohana


    【解决方案1】:

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

    但是,如果你不喜欢又快又脏,而更喜欢更好更具体,如果你使用Kohana 3.2,你可以执行以下操作.我没有在较旧或较新的版本上尝试过这个,所以它可能会或可能不会在它们中工作(如果您尝试将其移植到该版本,请咨询 transitioning document 相对于您希望移植的相关版本) :

    /**
    *      /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 短标签 - 但 do not use short tags in production code!):

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

    所有这些都有两个要点

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

    二:因此,此功能还为您提供了巨大的杠杆作用和力量,因为如果您有派生类,您可以实现与特定控制器类的相同类型的绑定,使其可用于任何后续派生类控制器类。 这样,如果您有两个类不应访问某些文件及其相关功能,例如一个管理 JavaScript 文件,可以加载某个用户的所有帖子,那么这种实现可以让你的生活变得更加轻松。

    并且,第三个隐藏选项是给 Kohana 是 PHP,如果您无法立即弄清楚,您可以在关联视图中使用常规的普通 PHP/HTML。虽然,我会劝你不要在生产代码中这样做。

    无论哪种方式,我希望这可以帮助你。

    【讨论】:

    • 在视图文件夹中的 kohana 中,我们有 .xhtml 文件,我创建了 view.xhtml 文件作为其他视图文件。在views/common 文件夹中有一个layout.xhtml 文件。它包含所有的 CSS 和 JS。在其他视图文件中,没有额外添加 layout.xhtml 文件编码。所以我没有添加任何额外的代码来将 layout.xhtml 文件包含到我的 view.xhtml 文件中。有没有办法做到这一点?
    • 好的,我想我现在明白你的问题了。在您的情况下,layout.xhtml 是模板视图。在您为新页面创建的视图中,例如rental.xhtml,您可以在内容中包含&lt;script&gt;&lt;style&gt; 标签以及相关的来源。但是,为了尽量减少猜测,请通过编辑您的问题发布该 rental.xhtml 文件的内容。
    • 这里可以下载3个文件...dangov.com/3-files.rar
    • @TrajkoDangov,我得到了它们,我目前正在查看它们。您应该从您的网站上删除该文件:3-files.rar。 ;)
    • 好的。您的文件确认 layout.xhtml 是您的模板。其他文件让我知道发生了什么。对于您的第一个问题:Kohana 没有内置方法可以简单地将脚本或样式添加到某些视图或页面。 如果样式或脚本是全局的(即必须访问无处不在),你可以把它放在模板中。如果它不是并且特定于一页,那么您有两个选择:将样式/脚本放在该特定视图中,或者将其绑定到控制器并从在视图中。
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多