简体   繁体   English

为Jade模板编码JavaScript

[英]encode javascript for a jade template

I am trying to pass a .js file to a jade template to have it render inside an ACE editor. 我正在尝试将.js文件传递给Jade模板,以使其在ACE编辑器中呈现。 But for some files (may have regex and other escaped characters) the editor will not render and I get errors in the browser, how should I encode/escape the file so that it is always rendered correctly? 但是对于某些文件(可能包含正则表达式和其他转义字符),编辑器将无法呈现,并且在浏览器中出现错误,我应该如何编码/转义文件,以便始终正确呈现? I have tried encode/decodeURIcomponent... I guess it's Jade that is causing the issue. 我已经尝试过encoding / decodeURIcomponent ...我猜是Jade引起了问题。

Or should I just try sending the file after the page renders and not have it expand as part of the template? 还是应该在页面渲染后尝试发送文件,而不将其扩展为模板的一部分?

Server: 服务器:

editFile = fs.readFileSync(fileName, 'utf8'); //JavaScript file
res.render('editor', { title: 'File Editor '+fileName, editFile: editFile });

Jade template: 翡翠模板:

#editor
script(src='/ace/ace.js', type='text/javascript', charset='utf-8')
script(type='text/javascript', charset='utf-8').
var fileContent = '!{editFile}';
var editor = ace.edit("editor"); //standard div
editor.getSession().setMode("ace/mode/javascript");
editor.setValue(fileContent);

So the solution seems to be to be: 因此,解决方案似乎是:

var fileContent = !{JSON.stringify(editFile)};

Thanks All. 谢谢大家

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

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