使用gulp-file-include从HTML模板到文件的相对路径
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
相关文章:
- jquery移动应用程序无法使用jquryMobile框架JS和css文件的相对路径
- 如何访问节点模块中具有相对路径的文件
- 在JQuery中,'加载'方法'的相对路径在引用项目中的html文件时开始
- 为什么浏览器可以'在`.中找不到js文件/lib/`(相对)位置
- gulp-useref – 不同级别文件夹中的相对输出路径
- 相对于文件系统而不是网站,源映射文件位置更少
- 无法使用 Jade 模板包含相对路径文件
- 如何使用它的相对路径访问tomcat服务器中的文件夹
- jQuery ajax 相对路径未找到文件
- 使用相对文件路径在 Google 地图中显示自定义标记
- 防止来自特定文件夹中模块的相对需求调用
- 如何使用JavaScript将文件保存在相对路径中
- 什么时候“;src”;在脚本标记中引用本地文件系统上的资源(相对于非本地文件系统)
- HtmlWebpackPlugin注入相对路径文件,这些文件在加载非根网站路径时会中断
- hta文件中的javascript用户配置文件相对路径
- 在文档根服务器上的任何CSS、JS、HTML、PHP和TPL文件上,将http替换为相对url//
- 如何不使window.location相对于文件
- 是否有一种标准的跨平台方式来处理和存储相对文件路径
- 包括相对于网站根的JS和CSS文件
- 如何加载常见的javascript文件在开发和web服务器上不使用相对路径