django模板 - 解析字符串变量内的变量 [英] django template - parse variable inside string variable

查看:338
本文介绍了django模板 - 解析字符串变量内的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个动态内容(从数据库)拉到模板中。你可以把它想象成一些简单的CMS系统。内容字符串包含一个模板变量。像这样(简体):

  vars ['current_city'] =伦敦
vars ['content '] ='当前城市是:{{current_city}}'#这个字符串来自db
返回render_template(request,'about_me.html',vars)

然后在模板中:

  {{content}} 

显然:
当前城市是:{{current_city}}


预计:

目前的城市是:London


$ b $我的问题 - 有什么办法在另一个变量内渲染变量名?使用自定义模板标记/过滤器似乎是一个好主意,但我试图创建一个没有成功...任何想法如何可以解决?

解决有一个自定义标签可能可以解决这个问题,但是它可能会有点复杂,因为有可能在一个模板中有一个完整的模板,因为没有任何限制你保存所有的模板数据库(可能包括其他模板标签)。我认为最简单的解决方案是从数据库手动呈现模板字符串,然后将其作为变量传递给主模板。

  from django.template import Template,Context 
...
context = {
'current_city':'London'
}
db_template = Template('current城市是:{{current_city}}')#从数据库中获取
context ['content'] = db_template.render(Context(context))
return render_template(request,'about_me.html',context )

注意:

遵循这条路,这可能不是很有效率,因为每次你将执行视图,数据库模板将不得不被编译。所以你可能想要缓存编译后的数据库版本,然后传递适当的上下文给它。以下是非常简单的缓存:

  simple_cache = {} 
$ b def fooview(request):
context = {
'current_city':'London'
}
db_template_string ='当前城市是:{{current_city}}'
如果simple_cache.has_key(db_template_string ):
db_template = simple_cache.get(db_template_string)
else:
simple_cache [db_template_string] =模板(db_template_string)
context ['content'] = db_template.render(Context )
return render_template(request,'about_me.html',context)


I'm pulling a dynamic content(from a database) to a template. You can think of this as some simple CMS system. The content string contains a template variable. Like this one (simplified case):

vars['current_city'] = "London"
vars['content'] = 'the current city is: {{current_city}}'  #this string comes from db
return render_template(request, 'about_me.html',vars)

then in template:

{{content}}

output obviously:
the current city is: {{current_city}}
expected:
the current city is: London

My question - is there any way to render a variable name inside another variable? Using custom template tag/filter seems to be a good idea but I was trying to create one with no success... Any ideas how this can be solved?

解决方案

Having a custom tag can probably solve this issue, however it might get a little complicated because then there is a possibility of having a full template within a template since nothing restricts you from saving all of your templates in db (which might include other template tags). I think the easiest solution is to manually render the template string from the db, and then pass that as variable to the main template.

from django.template import Template, Context
...
context = {
    'current_city': 'London'
}
db_template = Template('the current city is: {{current_city}}') # get from db
context['content'] = db_template.render(Context(context))
return render_template(request, 'about_me.html', context)

Note:

If you do follow this road, this might not be very efficient since every time you will execute the view, the db template will have to be compiled. So then you might want to cache the compiled version of the db and then just pass the appropriate context to it. The following is very simple cache:

simple_cache = {}

def fooview(request):
    context = {
        'current_city': 'London'
    }
    db_template_string = 'the current city is: {{current_city}}'
    if simple_cache.has_key(db_template_string):
        db_template = simple_cache.get(db_template_string)
    else:
        simple_cache[db_template_string] = Template(db_template_string)
    context['content'] = db_template.render(Context(context))
    return render_template(request, 'about_me.html', context)

这篇关于django模板 - 解析字符串变量内的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆