如何在模板中获取Kohana base_url

时间:2022-10-22 13:34:44

In Kohana 3 bootstrap.php one can define base_url:

在Kohana 3 bootstrap.php中,可以定义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.

这通常意味着还将/ js /,/ css /和其他媒体移动到该基础目录,如/ foo / js /,/ foo / css /。我的问题不是讨论这样的好坏。

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

在Kohana中是否有内置的方法可以从模板访问base_url(就像在Django中你可以使用{{MEDIA_URL}} css /)?

2 个解决方案

#1


6  

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

您可以使用URL :: base输出基本URL:

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

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

如果您输出相对于您可能需要URL :: site的URL:

<?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.

Kohana 3模板控制器使用View类来呈现模板。视图是普通的php文件,没有特殊的语法,所以只需使用正常的<?php ...?>标签。 View类允许您在呈现变量之前声明要在该视图中使用的变量。

#2


2  

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

一个好方法是在布局视图中,放在HTML头部的

标记附近:</p>
<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.

HTML 标记是一种为页面中的所有资产定义基本URL的方法。这样,您可以加载位于/foo/assets/images/img.jpg的图像,而无需在每个标记中进行URL :: base()调用。我希望它有所帮助。

#1


6  

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

您可以使用URL :: base输出基本URL:

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

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

如果您输出相对于您可能需要URL :: site的URL:

<?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.

Kohana 3模板控制器使用View类来呈现模板。视图是普通的php文件,没有特殊的语法,所以只需使用正常的<?php ...?>标签。 View类允许您在呈现变量之前声明要在该视图中使用的变量。

#2


2  

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

一个好方法是在布局视图中,放在HTML头部的

标记附近:</p>
<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.

HTML 标记是一种为页面中的所有资产定义基本URL的方法。这样,您可以加载位于/foo/assets/images/img.jpg的图像,而无需在每个标记中进行URL :: base()调用。我希望它有所帮助。