1

My website is www.kipclip.com and it's running on Kohana. I created a new rental page. But it's not taking my CSS and JS files. I tried to find how this is included or if Kohana has a special method for do that. But still not successful. Do you have any idea regarding this?

1 Answers1

0

A quick and dirty way to do so is to tack on the name of the script(s) and style(s) in the view you're implementing using regular html script and style tags and go on from there.

But, if you don't like quick and dirty, and prefer to do it better and more concrete, if you use Kohana 3.2, you can do the following. I haven't tried this on the older or newer versions, so it may or may not work in them (If you try to port it to that version, consult the transitioning document relative to the version in question that you wish to port):

/**
*      /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();
    }

}

So, how does this work? Remember that the application.php class is the base controller class from which all other controller classes are derived from. By implementing this type of binding to the base controller, every derived controller has access to what scripts, styles, etc. are available. And, as a result every associated view called by that controller also has access to those variables.

So, now to access those variables in your view:

Example, the template PHP file: /application/views/template.php

If it's defined like this (using PHP short tags - but 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>

There are two takeaway points from all of this:

One: If you want something to be global, or available to all controllers (and subsequent views), define them and bind them in the base application controller class.

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.

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • In our kohana in the view folder we have .xhtml files and I created the view.xhtml file as the other view files. In views/common folder there's a layout.xhtml file. It has all the CSS and JS includes. In other views files there is no extra addition of layout.xhtml file coding. So I didn't add any extra code to include the layout.xhtml file to my view.xhtml file. Is there a way to do that? – Trajko Dangov Aug 10 '13 at 06:23
  • Ok, I think I understand your problem now. In your case, `layout.xhtml` is the template view. In the view you've created for the new page e.g. `rental.xhtml`, you can include the ` – jrd1 Aug 10 '13 at 06:36
  • Here you can download the 3 files...dangov.com/3-files.rar – Trajko Dangov Aug 10 '13 at 07:17
  • @TrajkoDangov, I got them and I am currently looking at them. You should delete that file: `3-files.rar` from your website. ;) – jrd1 Aug 10 '13 at 07:26
  • Okay. Your files confirmed that `layout.xhtml` is your template. The other files give me an idea of what's going on. To your first question: **Kohana doesn't have a built-in method with which to simply add a script or style to some view or page.** If the style or script is global (i.e. has to be accessed everywhere), you can put it in the template. If it isn't and is specific **to one page**, then you have two options: **put the style/script in that specific view, or bind it to the controller and access the script name from within the view.** – jrd1 Aug 10 '13 at 07:33
  • @TrajkoDangov: So, you can add the script/style to the layout.xhtml as normal: `` for css, or as `` for JavaScript. Or, use that same code in the `view.xhtml`, to the top or bottom (where ever you want it). The view is interpreted as code before it is rendered in `layout.xhtml`. – jrd1 Aug 10 '13 at 07:40