如何使用Twig

时间:2017-12-10 03:13:07

标签: php html twig

我正在尝试让Twig编译模板并用绝对路径替换所有相对路径。

这是我的结构:

Source Directory Structure:
myFolder/
    content.txt
    image.png

Output Directory Structure:
index.html
myFolder/
    content.html
    image.png

content.txt文件包含:

Hello {{ twigvar }}
<img src="image.png" />

经过Twig后,content.html文件包含:

Hello varvalue
<img src="image.png" />

我有一个PHP应用程序,它从源myFolder获取内容,通过Twig模板传递content.txt,并将输出HTML写入输出目录中的myFolder/content.html。该脚本还将图像复制到输出目录。当我在浏览器中查看content.html页面时,图像会完美显示,正如您所期望的那样。

在此之前一切正常,因为我所做的就是编译模板并查看输出。

问题是,如何在index.html上显示HTML输出?由于图像路径是相对的,index.html不显示图像。

我希望Twig用绝对路径替换所有相对路径,因此content.html的输出将是这样的:

Hello varvalue
<img src="http://localhost/mywebsite/myFolder/image.png" />

当我调用http://localhost/mywebsite/myFolder/函数时,我会知道render字符串。我必须在源文件中使用相对路径,因为输出文件夹可能因站点配置而异。我也不能在源内容文件中使用任何模板变量,因为文件是用markdown编写的,并且编译为HTML。

0 个答案:

没有答案