PHP:Twig:图像的相对路径?
想知道是否有办法使用相对于 Twig 模板引擎中模板存储位置的路径。
场景如下:
我有一个 Typo3 网站,我的应用程序驻留在 fileadmin/myApplication 中。我使用 Twig 作为模板引擎来渲染从 JSON 文件动态加载的多语言内容。我的一些模板文件包含图像路径,考虑到 Typo3 的性质,这些图像需要有 fileadmin/myApplication/img/... 的 src 路径...
现在,如果我想测试应用程序的新版本,我想要能够创建目录 fileadmin/myApplication2.0 ,而无需更改模板文件中我的图像的路径。
有模板引擎(例如raintpl,请参阅此链接)将相对路径转换为服务器文件路径。有没有一种简单的方法可以在 Twig 中达到相同的效果?
例如
templates/template.html
img/logo.png
输出
<img src="fileadmin/myApplication2.0/img/logo.png">
rain.tpl 是这样做的:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
src
属性中的路径是相对 URL,而不是服务器上文件系统的相对文件路径,即您在目录中组织文件的方式。相对 URL 将解析为模板将属于/存在于其中的文档的基本 URL。因此您可以使用相对 URL,但您需要知道它们与什么相关才能让它们正常工作。
对于您的情况,快速解决方案可能是使用“
如果您的网站位于网络根目录中”。
另一种方法是使用一个模板函数,负责根据请求的 URL 路径构建(相对)URL。另一种方法是对
href <整个模板中的sup>Docs。另一种选择是获取渲染模板的输出,解析链接并使它们适合。
但重要的是您需要了解具体请求的 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
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.
正如约瑟夫所说,使用绝对路径:
只有当您的网站位于根网址上时,您才能看到图像,因为
它无法工作,
因此在这种情况下,您需要为其创建一个主机
当您可以看到时,模板就是所见即所得它在浏览器或 html 编辑器中的外观,基本上是所有使用相对路径的模板。
RainTPL 有一个很棒的想法,即用正确的服务器路径(相对或绝对)自动替换模板的相对路径,这样您就可以立即看到模板的外观。
使用所见即所得模板的另一个非常好的方法是
With absolute path as Joseph said:
you can see the images only if your website is on a root url as
it won't work on
so in this case you'll need to create an host for it
A template is WYSIWYG when you can see how it looks in your browser or in your html editor, so basically any templates that use relative paths.
RainTPL had the awesome idea to replace automatically the relative paths of the templates with the correct server path (relative or absolute), so you can see immediately how your template looks.
Another very good way to use WYSIWYG templates is the <base href="http://localhost/myApp/"> tag, which enables you to use relative paths. Only problem is the cross browsing and the Javascript because is not very clear if works the same in all of them.
资产路径将解析为 /web 目录。在我的示例中,图像的完整项目路径为:
您需要使用 .twig 扩展名才能使用此方法。
The asset path will resolve to the /web directory. In my example the full project path for the image would be:
You'll need to be using the .twig extension to use this method.