Question

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/)?

Was it helpful?

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.

OTHER TIPS

One good way is that in your layout view, in the head of the HTML you put near the <title> tag:

<base href="<?php echo URL::base(TRUE) ?>">

and then, you load your assets this way:

<img src="assets/images/img.jpg" alt="">

The HTML <base> tag is a way of defining a base URL for all the assets in the page. This way you load your image located at /foo/assets/images/img.jpg without making a URL::base() call in every tag. I hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top