基本上,我想用后端的初始脚本初始化我的编辑器(ace 编辑器)。但是,在某些情况下,initialScript还包括 (`)、(')、(") 字符。因此,我很困惑如何在不initialScript更改它们的情况下使用它。因为:当我将其解析initialScript为 html decode (html entities, like: `)时,在编辑器中也会显示`.var initialScript = `from django.utils.text import slugifyfrom django.utils.safestring import mark_safedef generate_unique_slug(klass, field): """ return unique slug if origin slug is exist. eg: `foo-bar` => `foo-bar-1` :param `klass` is Class model. :param `field` is specific field for title. """ origin_slug = slugify(field) unique_slug = origin_slug numb = 1 while klass.objects.filter(slug=unique_slug).exists(): unique_slug = '%s-%d' % (origin_slug, numb) numb += 1 return unique_slugbrian='Hello life!'`;也在我的编辑器配置中;var initialScript = `{{ session_initial_script|default:'' }}`;var editor = ace.edit('editor'); editor.setTheme('ace/theme/twilight'); editor.getSession().setMode('ace/mode/python'); editor.getSession().setUseWrapMode(true); editor.getSession().setValue(initialScript);有什么解决办法吗?
1 回答
大话西游666
TA贡献1817条经验 获得超14个赞
在特殊字符前使用反斜杠
var initialScript = `from django.utils.text import slugify
from django.utils.safestring import mark_safe
def generate_unique_slug(klass, field):
"""
return unique slug if origin slug is exist.
eg: \`foo-bar\` => \`foo-bar-1\`
:param \`klass\` is Class model.
:param \`field\` is specific field for title.
"""
origin_slug = slugify(field)
unique_slug = origin_slug
numb = 1
while klass.objects.filter(slug=unique_slug).exists():
unique_slug = '%s-%d' % (origin_slug, numb)
numb += 1
return unique_slug
brian='Hello life!'`
添加回答
举报
0/150
提交
取消