如何使模板扩展另一个? [英] How to make a Template extend another one?

查看:120
本文介绍了如何使模板扩展另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的数据库中存储的一些数据构建了一些模板:

  my_template = Template(string_data)

现在我想要该模板扩展另一个(也存储为文本DB)。我怎样才能做到这一点?寻找类似的东西:

  my_template.base = other_template 

或者任何语法。找不到这个文件。






我看到 template.nodelist ;我可以预先添加一些扩展节点吗?






添加了一个赏金...将授予第一个可以告诉的人我如何做到这一点没有黑客django的核心。

解决方案

不需要劫持Django的核心。像Ignacio所建议的那样,创建你自己的模板加载器很简单。您只需要将 django.template.loader.BaseLoader 进行子类化,并实现 load_template_source 方法。这只需要一个字符串的模板名称,您可以使用它来查找数据库中的项目并返回字符串。



在您的情况下,在单个数据库行中有两个模板元素,您可能需要在 extends 标记中进行指定:

  {%extendsmyinstance.html_version%} 

并简单地将它分成 load_template_source

  def load_template_source(self ,template_name,template_dirs = None):
name,field = template_name.split('。')
tpl = TemplateModel.objects.get(name = name)
return getattr(tpl,field )

当然,你想在那里放一些错误处理,但是你会得到想法。您只需在设置 TEMPLATE_LOADERS tuple中指定此模板加载器。



注释后编辑我还是不明白为什么,但是如果你想要做的就是选择模板从动态扩展,为什么不在处理之前插入文本模板?

  tpl_string = get_template_from_wherever()
extender ='{%extends%s%}'%template_name_to_extend
tpl = Template(extender + tpl_string)

仍然是一个黑客,但可能要比模板继承的内部伪装更少。 p>

I've got some templates which I'm building from some data I've got stored in my DB:

my_template = Template(string_data)

Now I want that template to extend another one (also stored as text in the DB). How can I do that? Looking for something like:

my_template.base = other_template

Or whatever the syntax is. Can't find documentation on this.


I see template.nodelist in the source; can I maybe prepend some kind of extends node?


Added a bounty... will grant to first person who can tell me how to do this without hacking the core of django.

解决方案

No need to hack the core of Django. Creating your own template loader, as Ignacio recommends, is pretty simple. You just need to subclass django.template.loader.BaseLoader, and implement the load_template_source method. This just takes a string for the template name, which you can simply use to look up the item in the database and return the string.

In your case, since you've got two template elements in the the single db row, you might want to do something to specify it in the extends tag:

{% extends "myinstance.html_version" %}

and simply split that in load_template_source:

def load_template_source(self, template_name, template_dirs=None):
    name, field = template_name.split('.')
    tpl = TemplateModel.objects.get(name=name)
    return getattr(tpl, field)

Of course you'd want to put some error handling in there, but you get the idea. You just then specify this template loader in your settings TEMPLATE_LOADERS tuple.

Edit after comment I still don't really understand why, but if all you want to do is to choose the template to extend from dynamically, why not just insert that in the text template before processing?

    tpl_string = get_template_from_wherever()
    extender = '{% extends "%s" %}' % template_name_to_extend
    tpl = Template(extender + tpl_string)

Still a hack, but probably much less of one than mucking about with the internals of template inheritance.

这篇关于如何使模板扩展另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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