[英]Relative path to a file from an HTML template using gulp-file-include
使用gulp-file-include從HTML模板中鏈接到CSS和JavaScript文件的推薦方法是什么?
例:
的index.html
@@include('templates/doc-begin.html')
<h1>Welcome</h1>
<p>Hello!</p>
@@include('templates/doc-end.html')
支持/ index.html的
@@include('templates/doc-begin.html')
<h1>Support</h1>
<p>RTFM</p>
@@include('templates/doc-end.html')
模板/ DOC-begin.html
<!doctype html>
<html lang=en>
<head>
<title>Website</title>
<link rel=stylesheet href=?????/style.css>
</head>
<body>
兩個index.html
文件位於不同的文件夾級別,因此link
標記中樣式表的相對路徑不能進行硬編碼(絕對路徑有自己的問題,在這種情況下不合適)。
基本上第一個link
標記需要是這樣的:
<link rel=stylesheet href=style.css>
第二個像:
<link rel=stylesheet href=../style.css>
在gulp-file-include中有一個方便的方法嗎?
注意:
我目前有一個解決方法,我將變量傳遞為"."
, ".."
或"../.."
進入每個@@include
電話,但這很麻煩而且很脆弱。
由於沒有回復,這是我的解決方法:
的index.html
@@include('templates/doc-begin.html', { "webRoot": "." })
<h1>Welcome</h1>
<p>Hello!</p>
@@include('templates/doc-end.html', { "webRoot": "." })
支持/ index.html的
@@include('templates/doc-begin.html', { "webRoot": ".." })
<h1>Support</h1>
<p>RTFM</p>
@@include('templates/doc-end.html', { "webRoot": ".." })
模板/ DOC-begin.html
<!doctype html>
<html lang=en>
<head>
<title>Website</title>
<link rel=stylesheet href=@@webRoot/style.css>
</head>
<body>
更新(2017年4月):
webRoot
功能已添加到gulp-file-include中 。
https://github.com/coderhaoxin/gulp-file-include#webroot-built-in-context-variable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.