我正在集成firepad与我的django项目。我在javascript方面没有太多的专业知识,因此我面临一个问题。
如果我遵循所有的步骤,一切似乎都很好。
<script>
//// Initialize Firebase.
//var firepadRef = getExampleRef();
// TODO: Replace above line with:
var ref = new Firebase('<YOUR FIREBASE URL>');
//// Create CodeMirror (with line numbers and the JavaScript mode).
var codeMirror = CodeMirror(document.getElementById('firepad-container'), {
lineNumbers: true,
mode: 'javascript'
});
//// Create Firepad.
var firepad = Firepad.fromCodeMirror(firepadRef, codeMirror);
//// Initialize contents.
firepad.on('ready', function() {
if (firepad.isHistoryEmpty()) {
firepad.setText('// JavaScript Editing with Firepad!\nfunction go() {\n var message = "Hello, world.";\n console.log(message);\n}');
}
});
</script>但是,在通过django视图呈现的页面上有一个名为“user”的字典变量。
我想在上面的例子中使用这个变量作为{user.textkey},而不是一个固定的默认文本。textkey指向文本。
user = {'textkey':text}就像这样:-
firepad.setText('{{user.textkey}}');我希望你明白我的意思。这里的问题是,文本可能包含任何类似撇号或unicode字符(如左或右单引号)等。而且django替换变量中的\n并导致javascript中不支持的格式。
例如:-
如果文本包含
Hello \xe2\x80\x98World\xe2\x80\x99:\n Good morning:\n 然后
firepad.setText('{{user.textkey}}');会像这样
firepad.setText('{{Hello 'World'
Good morning
}}');这是javascript无法理解的。
如何处理这件事?
欢迎任何建议。
发布于 2014-05-22 22:07:25
https://stackoverflow.com/questions/23817258
复制相似问题