Jade - Using Block inside script tag

2019-04-06 01:45发布

问题:

Hi there I'm trying using Jade's blocks and extends for a node.js project, and the ideia is to have something like this:

layout.jade:

head
    script
        $(document).ready(function() {
        block js_doc_ready
            //here goes the doc ready
        });

index.jade:

block js_doc_ready
    alert('hello!');
    alert('one more js line code');
    alert('end my js doc ready for this view');

This would give me a index.html like this:

...
<head>
    <script type="text/javascript">
            $(document).ready(function() {
                alert('hello!');
                alert('one more js line code');
                alert('end my js doc ready for this view');         
            });
    </script>
</head>
...

But when I see the result, the 'block js_doc_ready' is not considered a Jade block. Also, even if it was considered a block, the "alert('hello!);' wouldn't be considered a, but a Jade tag.

This is something that I used to do in django template, but in jade with all this tags, and no liberty to do pure html I still find it a little too strange to make this things.

回答1:

Jade do not translate what's inside 'style' and 'script' code. Never.

What will work is based on an answer I gave to another question (using a style element but that's basically the same).

!!!
head
  title Hello jade
  | <script type='text/javascript'>
  | $(document).ready(function() {
      block js_doc_ready
  | });
  | </script>

This way : jade will include the HTML 'script' tags and $.ready lines but will also include your block.