PHP:Twig:图像的相对路径? [英] PHP: Twig: Relative paths for images?

查看:29
本文介绍了PHP:Twig:图像的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有办法在 Twig 模板引擎中使用相对于模板存储位置的路径.

场景如下:

我有一个 Typo3 网站,我的应用程序位于 fileadmin/myApplication 中.我使用 Twig 作为模板引擎来呈现从 JSON 文件动态加载的多语言内容.我的一些模板文件包含图像的路径,鉴于 Typo3 的性质,需要具有 fileadmin/myApplication/img/... 的 src-path

现在,如果我想测试我的应用程序的新版本,我希望能够创建目录 fileadmin/myApplication2.0,而无需更改模板文件中图像的路径.

有模板引擎(例如raintpl,请参阅在整体模板中.

另一种选择是获取渲染模板的输出,解析链接并使它们适合.

但重要的是,您需要具体了解所请求的 URL 路径以及您的模板(块)的使用方式.

Wondering if there is a way to use paths relative to the template storage location in the Twig templating engine.

The scenario is as follows:

I have a Typo3 website where my application resides in fileadmin/myApplication. I am using Twig as a template engine to render multilingual content that is loaded dynamically from JSON files. Some of my template files contain paths to images that, given the nature of Typo3, need to have a src-path of fileadmin/myApplication/img/...

Now, if I want to test a new version of my application, I want to be able to create a directory fileadmin/myApplication2.0 without having to change the paths to my images inside the template files.

There are templating engines (e.g. raintpl, see this link) that translate relative paths to server file paths. Is there an easy way of achieving the same effect in Twig?

e.g.

templates/template.html 
img/logo.png

outputs

<img src="fileadmin/myApplication2.0/img/logo.png">

This is how rain.tpl does it:

 WYSIWYG - Path replace
This cool feature allows designers to create templates as regular HTML with images and styles with relative paths, RainTPL replaces automatically these paths with the correct server paths.

Absolute paths and paths ending with # will be not changed.

<link href="style.css" type="text/css" rel="stylesheet">
<img src="img/logo.gif">

Output html:

<link href="tpl/style.css" type="text/css" rel="stylesheet">
<img src="tpl/img/logo.gif">

Note: if you set raintpl::$base_url, RainTPL will replace the path with raintpl::$base_url.

解决方案

The path in the src attribute is a relative URL, not a relative file path to the file system on your server, how you organize your files inside your directories.

The relative URL will be resolved to the base URL of the document the template will be part of / present in. So you can use relative URLs, but you need to know to what they relate to to have them properly working.

In your case a quick solution might be to use

<img src="/img/logo.png">

If your website resides in the web-root.

Another way is to have a template function that takes care to build the (relative) URL according to the requested URL path. Another approach is to hard-encode a <base> href Docs in the overall template.

Another alternative is that you get the output of the rendered templates, parse the links and make them suit.

But the important part is that you need to know about the requested URL path in specific and how your template (blocks) are being used.

这篇关于PHP:Twig:图像的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆