Django模板剥离空格?

Django模板剥离空格?,第1张

Django模板剥离空格

首先,我要说@DNS的答案是正确的,为什么空格没有显示出来。

考虑到这一点,此模板过滤器会将字符串中的所有空格替换为

 

用法:

{{ "hey there  world"|spacify }}

输出将是

hey there  world

这是代码:

from django.template import Libraryfrom django.template.defaultfilters import stringfilterfrom django.utils.html import conditional_escapefrom django.utils.safestring import mark_safeimport reregister = Library()@stringfilterdef spacify(value, autoescape=None):    if autoescape:    esc = conditional_escape    else:    esc = lambda x: x    return mark_safe(re.sub('s', '&'+'nbsp;', esc(value)))spacify.needs_autoescape = Trueregister.filter(spacify)

有关模板过滤器如何工作以及如何安装的注释,请参阅docs。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存