How do I pass variable from a Jade template file to a Javascript file?

穿精又带淫゛_ 提交于 2019-12-01 11:50:11

Try adding a . character after the script, this tells Jade that you want a block of text inside a tag.

Here's your updated code.

html
    head
        script(type='text/javascript').
            var data = !{data}
        link(rel='stylesheet', href='/stylesheets/style.css')
    body
        title Title
        h1 Heading
        #div.test

EDIT - To set title to data.val

First you should change your response, remove JSON.stringify so you're returning an object not a string value.

res.render("aws.jade", {data : {'val' : 'This is a Test'}});

Then you should be able to access the data objects attributes and structure directly using dot notation, and assign it straight to the title element like this...

title= data.val
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!