简体   繁体   English

如何在Jade模板中使用Javascript变量?

[英]How to use Javascript Variables in Jade Template?

I am using IBM Bluemix to make a web service for a school project. 我正在使用IBM Bluemix为学校项目制作Web服务。

My project needs to request a JSON from an API, so I can use the data it provides. 我的项目需要从API请求JSON,因此我可以使用它提供的数据。

I want the Jade Template to write strings using the data that come from the API, but I can't use the variables that are defined in my .js file. 我希望Jade Template使用来自API的数据来编写字符串,但我不能使用我的.js文件中定义的变量。

For instance, I would like to write 例如,我想写

"Your Exchange Rate is"+CotacaoDolar “你的汇率是”+ CotacaoDolar

in the h1 field of the jade file, being "CotacaoDolar" a variable defined in the .js file. 在jade文件的h1字段中,“CotacaoDolar”是.js文件中定义的变量。

How could this be done? 怎么可以这样做?

Here is my .js file: 这是我的.js文件:

 /*eslint-env node*/ //------------------------------------------------------------------------------ // node.js starter application for Bluemix //------------------------------------------------------------------------------ // HTTP request - duas alternativas var http = require('http'); var request = require('request'); // cfenv provides access to your Cloud Foundry environment // for more info, see: https://www.npmjs.com/package/cfenv var cfenv = require('cfenv'); //chama o express, que abre o servidor var express = require('express'); // create a new express server var app = express(); // serve the files out of ./public as our main files app.use(express.static(__dirname + '/public')); // get the app environment from Cloud Foundry var appEnv = cfenv.getAppEnv(); // start server on the specified port and binding host app.listen(appEnv.port, '0.0.0.0', function() { // print a message when the server starts listening console.log("server starting on " + appEnv.url); }); app.get('/home1', function (req,res) { http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) { var body = ''; res2.on('data', function (chunk) { body += chunk; }); res2.on('end', function () { var json = JSON.parse(body); var cotacao = json["bovespa"]["cotacao"]; var CotacaoDolar = json["dolar"]["cotacao"]; var VariacaoDolar = json["dolar"]["variacao"]; var CotacaoEuro = json["euro"]["cotacao"]; var VariacaoEuro = json["euro"]["variacao"]; var Atualizacao = json["atualizacao"]; res.render('cotacao_response.jade', { message: 'A taxa de câmbio é de '+CotacaoDolar+' R$.Esse valor corresponde ao preço de venda em '+Atualizacao }); }); }); }); 

Here is my .jade file: 这是我的.jade文件:

 doctype html html(lang="en") head title Cotação link(rel='stylesheet',href='stylesheets/style.css') body h1!=message #container.col p Seu câmbio foi. p. 

You just need to pass in an object when rendering the template which holds your variables. 您只需在渲染保存变量的模板时传入一个对象。 The value is then accessible by referencing the key in your Jade template. 然后,可以通过引用Jade模板中的键来访问该值。 So to send your variable 'CotacaoDolar' you would pass in the object as such: 因此,要发送变量'CotacaoDolar',您将传递对象:

res2.render('cotacao_response.jade', {
    'CotacaoDolar': CotacaoDolar
});

Then in your Jade template you can create the h1 like: 然后在您的Jade模板中,您可以创建h1,如:

h1 Your exchange rate is #{CotacaoDolar}

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

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