简体   繁体   English

链接的JavaScript文件的帕格字符串插值

[英]Pug string interpolation for linked JavaScript files

What I'm Trying To Do 我要做什么

I have a NodeJS webserver that serves an HTML and JavaScript file using Express and Pug . 我有一个NodeJS网络服务器,该服务器使用ExpressPug提供HTML和JavaScript文件。 It looks something like this: 看起来像这样:

index.pug index.pug

html
    head
        title Hello
    body
        h1 Hello World!
        script(src='script.js')

app.js app.js

const express = require('express');
var app = express();
app.set('view engine', 'pug');

app.get("/", (req, res) => {
    res.render("index", {
        age: 91,
        name: 'George P. Burdell'
    });
});
app.listen(8080);

script.js script.js

var person = {
    age: #{age},
    name: #{name}
};

(Taken from and added on to GitHub example ) (源自并添加到GitHub示例中

I want to be able to swap the #{age} and #{name} placeholders with the values indicated in the res.render() function (ie {age: 91, name: 'George P. Burdell'} ). 我希望能够将#{age}#{name}占位符交换为res.render()函数中指定的值(即{age: 91, name: 'George P. Burdell'} )。

The Problem 问题

As of right now, the values are not swapped and the console spits out an error indicating the lines with placeholders #{age} and #{name} are not recognized. 截至目前,这些值尚未交换,控制台吐出一个错误,指示无法识别带有占位符#{age}#{name}的行。 The placeholder values are swapped only if the JavaScript code in script.js are brought into the .pug file as an internal script like so: 仅当将script.js中的JavaScript代码作为内部脚本带入.pug文件时,才交换占位符值,如下所示:

index.pug index.pug

html
    head
        title Hello
    body
        h1 Hello World!
        script.
            var person = {
                age: #{age},
                name: #{name}
            };

But what I want to be able to do is swap the values directly from an external script file such as in the initial setup. 但是我想要做的是直接从外部脚本文件交换值,例如在初始设置中。 I've had no luck finding an easy way to do this apart from having the code "live" inside the .pug file. 除了将代码“实时”保存在.pug文件中之外,我还没有找到一种简单的方法来运气。

So I have a workaround. 所以我有一个解决方法。 What you can do is set global variables in an internal script tag in the .pug file which can then be interpolated using pug. 您可以在.pug文件的内部脚本标记中设置全局变量,然后可以使用pug对其进行插值。 As a visual, here's what I mean: 作为视觉效果,这就是我的意思:

index.pug index.pug

html
    head
        title Hello
    body
        h1 Hello World!
        script.
            var age = #{age};
            var name = #{name};
        script(src='script.js')

script.js script.js

var person = {
    age: age, // the age variable from the internal script is used
    name: name // the name variable from the internal script is used
};

app.js remains the same! app.js保持不变!

EDIT 编辑

app.js app.js

res.render("index", {
    age: 91,
    name: 'George P. Burdell'
});

should be 应该

res.render("index", {
    age: "91",
    name: "'George P. Burdell'"
});

to keep the data type after swapping! 交换后保持数据类型!

Please let me know if there is an even better way to solve this! 如果有更好的解决方法,请告诉我! For now, this is the accepted answer. 目前,这是公认的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM