如何使用 gulp-file-include 测试上下文变量是否存在

Ber*_*ram 3 testing if-statement gulp

我正在gulp-file-include使用一些部分和模板来构建我的 html 页面。通过使用上下文变量,我可以自定义每个meta标题。但是,不知道如何仅在上下文变量存在时才包含一行,因为“@@else”语句似乎不存在。

我的父级 HTML 如下所示:

@@include ('_header.html', {
    "title":"my page",
    "description": "description",
    "canonical":"http://www.sourcefromquote.com" })

<body>
    A wonderful Page 
    @@include ('_footer.html")
</body></html>
Run Code Online (Sandbox Code Playgroud)

我正在考虑使用_header.html类似这样的 close :

<html>
<head>
    <title>@@title</title>
    <meta name="description" content="@@description">
    @@if (canonical) {  <link rel="canonical" href="@@canonical" /> }
</head>
Run Code Online (Sandbox Code Playgroud)

如果父 HTML 中未设置“规范”变量,则会抛出错误 ( canonical is not defined)。

我想我可以在变量中包含完整的标签并忘记@@if,但这不会像预期的那么干净!

有任何想法吗 ?先感谢您。

小智 5

在头部,您输入:

@@if (context.canonical) {<link rel="canonical" href="@@canonical" />}
Run Code Online (Sandbox Code Playgroud)

在包含您输入的标头的文件中:

@@include('_head.html', {
  "canonical" : "https://www.website.com/canonical-link.html"
})
Run Code Online (Sandbox Code Playgroud)