How to create a template for another?

I have some templates that I create from some data that I saved in my database:

my_template = Template(string_data)

Now I want this template to expand another one (also saved as text in the database). How can i do this? Looking for something like:

my_template.base = other_template

Or regardless of syntax. Cannot find documentation on this.


I see template.nodelist in the source; can i possibly add some node extensions?


Added generosity ... will provide the first person who can tell me how to do this without breaking the django core.

+3
source share
5 answers

​​Django. , , . django.template.loader.BaseLoader load_template_source. , .

, db, -, extends:

{% extends "myinstance.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)

, , , , .

+3

, :

  • , ( ),
  • , ( , ),
  • ,
  • ,
  • ( ) ( ),
  • .
  • , - {% extends ...%} , ... ?
  • , , Django.

:

. Django , .

:

Django extends. , ExtendsNode django.template.loader_tags, extends. (), ( - , ). , , , , .

, , render() node . node - ExtendsNode ( node), . ExtendsNode , ( (parent_name), (parent_name_expr). ExtendsNode , get_parent() get_template (parent_name), . , ExtendsNode:: render() .

.

, :

  • SpecialExtendsNode (ExtendsNode), __init__ get_parent.
  • , .
  • SpecialExtendsNode - .
  • , Django .

, , :

class SpecialExtendsNode(ExtendsNode):

    def __init__( self, nodelist, parent, name ):
        self.myparent = parent
        ExtendsNode.__init__( self, nodelist, name, None, None )

    def get_parent( self ):
        return self.myparent

:

parent = Template( parent_string )
child = Template( child_string )
hack = SpecialExtendsNode( child.nodelist, parent, "parent name" )
child.nodelist.insert( 0, hack )
output = child.render( context )

, , , , , , .

, , Django 1.2. , 99% , Django 1.2. , Django. , Django , ( , ). , , - Django . , 100% , . ? . ​​Django. , ( , ) Django 1.2 , . , , , Django , ?

, Django - , ?

+2

, django.template.loader_tags.ExtendNode get_parent, get_template ! ExtendNode template.nodelist, , !

+1
from app.models.misc import EmailTemplate
from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader

class Loader(BaseLoader):
    is_usable = True
    def load_template_source(self, template_name, template_dirs=None):
        try:
            key, ext = template_name.split('.')
            attr = {
                'html': 'html_content',
                'txt': 'text_content',
            }[ext]
            tpl = EmailTemplate.objects.get(key=key)
            return (getattr(tpl, attr), repr(tpl))
        except:
            raise TemplateDoesNotExist

It is really hard to write if you cannot find the updated documentation: \

0
source

Source: https://habr.com/ru/post/1753820/


All Articles