PHP模板系统、图像路径

发布于 2024-10-06 09:13:33 字数 311 浏览 0 评论 0原文

我正在用 PHP 制作一个基本的模板系统,问题是:

我有一个主请求处理程序,所有内容都从这里加载和处理,但由于我下载的 CSS 模板内的路径不再正确。

示例:

<img src="assets/images/contact.gif" />

必须是:

<img src="templates/grey-box/assets/images/contact.gif" />

有什么方法可以解决这个问题吗?PHP 明智的吗?

I'm making a basic templating system in PHP, the problem is:

I have a main request handler where from where everything is loaded and processed, but because of that the paths inside the CSS template I downloaded aren't correct anymore.

Example :

<img src="assets/images/contact.gif" />

has to be:

<img src="templates/grey-box/assets/images/contact.gif" />

Is there any way to fix this, PHP wise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-10-13 09:13:33

不要偷懒

  1. 始终使用绝对路径

  2. 使其明确,而不是使用一些肮脏的技巧。这将是一场支持噩梦。

  3. 您可以使用一些辅助变量,例如 $tpl_assets_path 并在模板中使用它。

绝对路径从 / 开始,其中 / 指定网站根目录。
HTML 页面上的每个路径都应该如此。

所以,模板应该是

<img src="/templates/grey-box/assets/images/contact.gif" />

或者

<img src="<?=$tpl_assets_path?>images/contact.gif" />

Don't be lazy

  1. Always use an absolute path.

  2. Make it explicit, not with some dirty hacks. It will be a support nightmare.

  3. You can use some helper variables, like $tpl_assets_path and use it in the template.

Absolute path starts from / where / designates web-seite root.
And every path on your HTML page should be.

So, the template should be either

<img src="/templates/grey-box/assets/images/contact.gif" />

or

<img src="<?=$tpl_assets_path?>images/contact.gif" />
黎夕旧梦 2024-10-13 09:13:33
define('HTTP_SERVER', 'http://www.myhomeurl.com/');
<img src="<?php echo HTTP_SERVER?>assets/images/contact.gif />
define('HTTP_SERVER', 'http://www.myhomeurl.com/');
<img src="<?php echo HTTP_SERVER?>assets/images/contact.gif />
萝莉病 2024-10-13 09:13:33

它在这里

$imgarray = array('assets','script','css');
$tpl = '/templates/grey-box/';
foreach($imgarray as $i){
  $tpldir = $tpl.$i;
  $str = preg_replace("/([^\/])($i)([\/])/i","\$1$tpldir\$3",$str);
}

it's here

$imgarray = array('assets','script','css');
$tpl = '/templates/grey-box/';
foreach($imgarray as $i){
  $tpldir = $tpl.$i;
  $str = preg_replace("/([^\/])($i)([\/])/i","\$1$tpldir\$3",$str);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文