想知道是否有方法可以使用相對於Twig模板引擎中模板存儲位置的路徑。PHP:Twig:圖像的相對路徑?
的情況如下:
我有一個TYPO3的網站,我的應用程序駐留在fileadmin/MyApplication的。我使用Twig作爲模板引擎來呈現從JSON文件動態加載的多語言內容。我的一些模板文件包含路徑圖像,鑑於Typo3的性質,需要有一個src路徑fileadmin/myApplication/img/...
現在,如果我想測試我的新版本應用程序,我希望能夠創建一個目錄fileadmin/myApplication2.0,而不必更改模板文件中的圖像路徑。
存在將相對路徑轉換爲服務器文件路徑的模板引擎(例如raintpl,請參閱this link)。是否有一種在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.
hakre,感謝您的回答。我想我的例子過於簡單了。我會盡量在我編輯的答案中更具體。 –
我看到你編輯的答案,但它沒有改變。我發現它不適合你。您首先需要了解哪些網址適用,以及相關網址的工作方式。 IIRC TYPO3的利用了'的<基本href =「」>'在它的輸出已經(或這是很常見之間TYPO3項目。 – hakre
有你居然看着到rain.tpl的聯繫?難不成我不知道第一關於URL的事情並不能真正幫助 –