1

Is there a way to inline Javascript code in Jade script tag while maintaining indentation?

Removing indentation works but it makes code extremely difficult to read and modify.

Another option is to put Javascript in separate files, but then we need to pass a few parameters from the template to the script. Is it possible to do that in "separate files" approach?

missingfaktor
  • 90,905
  • 62
  • 285
  • 365

1 Answers1

1

From the Tag Text section of the Jade user documentation, there are two ways to include large blocks of text in tags. The following example demonstrates both ways for the "script" tag. Jade handles the indented code just fine.

script.
  console.log("One way to preserve indentation");
  if (true) {
     console.log("This is indented");
  }
  console.log("Different indentation");

script
  console.log("Another way to preserve indentation");
  if (true) {
     console.log("This is indented");
  }
  console.log("Different indentation");
Will Nelson
  • 966
  • 8
  • 13
  • I am using Jade4J actually. Does it not implement this Jade feature? Do you have any idea about that? – missingfaktor Feb 14 '13 at 09:12
  • I haven't used Jade4J. Perhaps this is a bug? I suggest trying other tags, e.g., "p" with varying indentation and checking the page source to see if the output is correct. A possible workaround: How about if you prepend the script lines with "|"? In the Javascript version of Jade (by visionscript), this works for tags like "p", but _not_ the script tag. Perhaps it works on Jade4J? – Will Nelson Feb 14 '13 at 09:32
  • I just tried using Jade4J 0.3.8. It worked fine for me. In particular, the input ` script. foo bar baz script foo bar baz` generates acceptable output. – Will Nelson Feb 14 '13 at 10:47
  • The code snippet in my last comment isn't displaying properly. I used varying indentation exactly as in my original answer and it worked fine under Jade4J 0.3.8. – Will Nelson Feb 14 '13 at 10:55