Bertrand Engogram
Bertrand Engogram

Reputation: 629

how to test existence of context variable using gulp-file-include

I am using gulp-file-include to build my html pages using some partials & templates. By using context variables, I can customize each meta headers. However, don't know how I could include a line only if a context variable exists, as the "@@else" statement doesn't seem to exist.

My parent HTML looks like:

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

<body>
    A wonderful Page 
    @@include ('_footer.html")
</body></html>

I was thinking to use a _header.html close to something like that :

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

If the "canonical" variable is not set in the the parent HTML, it throws an error (canonical is not defined).

I guess I could include the full tag in a variable and forget about the @@if, but that would not be as clean as expected !

Any ideas ? Thank you in advance.

Upvotes: 3

Views: 2780

Answers (1)

Aleksandar Olic
Aleksandar Olic

Reputation: 121

In the head, you enter:

@@if (context.canonical) {<link rel="canonical" href="@@canonical" />}

In the file that includes the header you enter:

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

Upvotes: 5

Related Questions