1

I am using Jade just to include other sections in my HTML files using include. This works fine if I write Jade syntax instead of HTML syntax. But in my case, I need to write HTML syntax only. I am trying to use jade just for include only.

From this link, I found that we can write HTML by including . or | in the code. So, to test this out, I wrote the code like this:

File index.jade

div.main.
     <div class="wrapper">
         include header
     </div>

As you can see in above code, I added . as suffix to the Jade syntax line, i.e., div.main., which lets me write HTML from next line.

File header.jade

<header></header>

But this isn't working. The rendered HTML looks like this:

File index.html

<div class="main">
    <div class="wrapper">
         include header
    </div>
</div>

If I don't use . and follow the Jade syntax, everything works fine. But in my case, I really need to write in HTML, but not in Jade.

Is there a workaround to make the include work inside the HTML syntax?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • 1
    Using `.` or `|` will simply output exactly what you write, that's why you're seeing `include header` in the resulting HTML. I don't think there's a way around it. Can I ask why you need to write in HTML? – SlashmanX Aug 26 '15 at 08:39
  • @SlashmanX I have a silly requirement. This is actually meant to provide to Freshers who has no idea about HTML. Learning jade without HTML is not good in this case. Actually, I will give small sections to design to the freshers and then I will use `include` to gather those files on single page. Even I am willing to write in HTML, so that they will not get confuse. There are many pages, so copying and pasting in all the files when a section looked overkill to me. – Mr_Green Aug 26 '15 at 09:01
  • Maybe I should look for other alternatives which let me write in HTML and also use a feature like `include` of jade. Any suggestions please? – Mr_Green Aug 26 '15 at 09:03

2 Answers2

4

Well, it is possible to do what you want, but I am not sure if Jade is the best option.

Note: In Jade, every line which starts with < is considered plain text, so there is no need to use dot or | to write html tags.

Here is a working example of what you want:

a.jade

div.main
  <div class="wrapper">
  include b.jade
  </div>

b.jade

<div class="b">I am content from b.jade</div>

and after compilation of a.jade we get:

a.html

<div class="main">
  <div class="wrapper">
    <div class="b">I am content from b.jade</div>
  </div>
</div>

This code was tested and it works 100% with the latest version of Jade, but It works only when you don't increase indentation level. For example, the following code will not work:

div.main
  <div class="wrapper">
    include b.jade
  </div>

On compilation it will throw: unexpected token "indent", and the error itself:

div.main
  <div class="wrapper">
    include b.jade
  ^^ extra indent "tab"
  </div>

The same is true for nested plain HTML too, so the following:

div.main
  <div class="wrapper">
    <div class="foo">
      include b.jade
    </div>
  </div>

will also throw this error: unexpected token "indent", and the errors:

div.main
  <div class="wrapper">
    <div class="foo">
  ^^
      include b.jade
  ^^^^
    </div>
  ^^
  </div>

You can write code like this:

div.main
  | <div class="wrapper">
  |  <div class="foo">
  |    <div class="bar">
  include b.jade
  |    </div>
  |  </div>
  | </div>

and assuming that we already have that b.jade, it will be compiled into:

<div class="main">
  <div class="wrapper">
   <div class="foo">
     <div class="bar"><div class="b">I am content from b.jade</div></div>
   </div>
  </div>
</div>

But note where I placed that include b.jade, exactly one tab has been added in comparison with last Jade command div.main (so included file will be nested into .main div), and you should follow that indent rule if you want your code to work.


Alternative solution

As I wrote at the beginning, Jade is not the best option in your case. I would use another server side language to do what you want.

Here is a basic algorithm:

  1. Write your HTML files in plain HTML (.html) and as an include use a custom tag like <include b.html>
  2. Create a master file using a server side language which will load and process your HTML files and will replace your custom tags with actual content from these files
  3. Save output to a new file and use it.

Here is an example written in PHP:

master.php

<?php

$main_file = "a.html";
$content = file_get_contents($main_file);

$content = preg_replace_callback(
    '!<include\s+([^>]+)>!',
    function ($m) {
        return file_get_contents($m[1]);
    }, $content
);

file_put_contents("bundle.{$main_file}", $content);

Now HTML files:

a.html

<div class="main">
    <div class="wrapper">
        <include b.html>
    </div>
</div>

b.html

<div class="b">foobar</div>

Now after we will execute master.php we will get bundle.a.html with the following content:

bundle.a.html

<div class="main">
    <div class="wrapper">
        <div class="b">foobar</div>
    </div>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John
  • 7,500
  • 16
  • 62
  • 95
  • I tried this case too. It didn't work. btw, there should not be a suffix `.` in this case. – Mr_Green Aug 26 '15 at 08:51
  • Yes, you're right, I forgot to remove "." , just updated my answer – John Aug 26 '15 at 08:52
  • I had actually tried this and somehow, it isn't working for me. Maybe the error is somewhere else. – Mr_Green Aug 26 '15 at 09:00
  • @Mr_Green make sure you don't have extra indentation before your include – John Aug 26 '15 at 09:02
  • @John I tried removing space. Here is the codepen. http://codepen.io/Mr_Green/pen/LVwoEa – Mr_Green Aug 26 '15 at 09:05
  • @Mr_Green you have extra tab before include. When you switch back from plain HTML to jade, jade command must have at most one "tab" compared to the last jade command – John Aug 26 '15 at 09:10
  • @John can you please do those changes and provide the same codepen? I tried here with different spaces/tabs. Still I can't get rid of that error. – Mr_Green Aug 26 '15 at 09:16
  • @robertklep Please check the codepen in above comments where this isn't working even if I remove spaces. – Mr_Green Aug 26 '15 at 09:32
  • @Mr_Green you need to provide a `filename` option if you want to use include's. I have no idea how to set that option on Codepen, but in Express (if you happen to use that) the option will be set automatically. [Here is a gist](https://gist.github.com/robertklep/10b514445e9afac29b9c) for a standalone version. – robertklep Aug 26 '15 at 09:40
  • @John Thanks for the detailed answer. but actually I found [another solution](http://stackoverflow.com/a/32228623/1577396) which is perfect for my requirement. – Mr_Green Aug 26 '15 at 13:56
0

I get stuck by the same problem. Jade requires me to use no indent in the plain HTML. But if you change the header.jade to header.html, it will work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shaosh
  • 657
  • 2
  • 8
  • 30