Jade - 包含来自外部js文件的var?

时间:2021-11-05 09:50:48

Jade newbie here.

翡翠新手在这里。

I'm aware that in Jade you are allowed to create js variables and interpolate them in seamlessly with html like so:

我知道在Jade你可以创建js变量并与html无缝插入它们,如下所示:

- var myname = "john"
p my name is #{myname}

However what if that variable were to come from an external .js file (or, if impossible, an external .jade file)?

但是,如果该变量来自外部.js文件(或者,如果不可能,则是外部.jade文件)该怎么办?

file.js:

var myname = "john";

and then the index.jade:

然后是index.jade:

- include file.js
p my name is #{myname} //this does not work

I'm not sure whether the render function has anything to do with this. If someone could be so generous as to explain this in plain English, it would be greatly appreciated.

我不确定渲染功能是否与此有关。如果有人能够如此慷慨地用简单的英语解释这一点,我将不胜感激。

2 个解决方案

#1


1  

include just includes the raw text if the file is not a Jade file. More info

include如果文件不是Jade文件,则只包含原始文本。更多信息

So - include file.js wouldn't parse any of it's content.

所以 - 包含file.js不会解析它的任何内容。

It should work for another Jade file though.

它应该适用于另一个Jade文件。

#2


0  

I think what most people do is to throw in variable:value pairs at the call to render:

我认为大多数人所做的是在调用render时抛出变量:值对:

  router.get('/', function(req, res, next) {
    res.render('index', { myname: 'John' });
  });

...and then use them as inline variables as you did earlier.

...然后像以前一样使用它们作为内联变量。

#1


1  

include just includes the raw text if the file is not a Jade file. More info

include如果文件不是Jade文件,则只包含原始文本。更多信息

So - include file.js wouldn't parse any of it's content.

所以 - 包含file.js不会解析它的任何内容。

It should work for another Jade file though.

它应该适用于另一个Jade文件。

#2


0  

I think what most people do is to throw in variable:value pairs at the call to render:

我认为大多数人所做的是在调用render时抛出变量:值对:

  router.get('/', function(req, res, next) {
    res.render('index', { myname: 'John' });
  });

...and then use them as inline variables as you did earlier.

...然后像以前一样使用它们作为内联变量。