css - 为什么 Kohana 不读我的 CSS?

标签 css kohana

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

最佳答案

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

但是,如果您不喜欢快速和肮脏,并且更喜欢更好和更具体,如果您使用 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 类是 Controller 的基类,所有其他 Controller 类都派生自该类。 通过对基本 Controller 实现这种类型的绑定(bind),每个派生 Controller 都可以访问可用的脚本、样式等。因此,该 Controller 调用的每个关联 View 也可以访问这些变量。

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

例如,模板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>

所有这一切有两个要点:

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

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

并且,第三个隐藏选项是 Kohana 是 PHP,如果您不能立即弄清楚,您可以在关联 View 中使用普通的 vanilla PHP/HTML。不过,我会劝阻您不要在生产代码中这样做。

不管怎样,我希望这对你有所帮助。

关于css - 为什么 Kohana 不读我的 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18153932/

相关文章:

view - 如何开始使用 Kostache?

html - 悬停图像时出现奇怪的星号?

php - dompdf HTML to PDF - 无法设置页边距

html - CSS3 : parent element has hover, 动画元素里面

html - float :right moves other divs down

mysql - Kohana 3.2 选择问题

php - 上传到亚马逊 S3 - kohana

php - 通过 ORM 将扩展的 Kohana DB 类用于多个数据库

javascript - 函数内的警报和函数调用发生故障

linux - Kohana_HTTP_Exception [404] : The requested URL/was not found on this server