Search code examples
phptemplateskohanabase-url

How to get Kohana base_url in template


In Kohana 3 bootstrap.php one can define base_url:

Kohana::init(array(
    'base_url'   => '/foo/',
));

This usually means also moving the /js/, /css/ and other media to that base dir like /foo/js/, /foo/css/. My question is not to discuss good or bad of such.

Is there a built-in way in Kohana to access the base_url from a template (just like in Django you can use {{ MEDIA_URL }}css/)?


Solution

  • You can output the base url as using URL::base:

    <?php echo URL::base(); ?>
    

    If you're outputting a url relative to that you probably want URL::site:

    <?php echo URL::site('css/'); ?>
    

    Kohana 3 template controllers use the View class to render templates. Views are normal php files and have no special syntax, so just use the normal <?php ... ?> tags as above. The View class allows you to declare variables for use in that view, before you render it.