I'm interested in participating  in your mentors program in Google
Summer Code.
Looking at the template implementation I've seen that I can make some
solutions  for implementation compiling in bytecode templates. Just
adding function «compile» to every Node-derived class and modifying/
adding new functionality to internal functions,classes, such as
django.template.Variable, django.template.FilterExpression, and
others. E.g. Node-derived classes :

class TextNode(Node):
    def compile(self):
        return "print \"\"\"%s\"\"\";\n"  % self.s

class Variable(object):
    def compile(self):
        print "resolve(context,%s)" % self.lookups

class FilterExpression(object):
    def compile(self, ignore_failures=False):
        if isinstance(self.var, Variable):
            try:
                obj = self.var.compile()
            except VariableDoesNotExist:
                if ignore_failures:
                    obj = None
                else:
                    if settings.COMPILED_TEMPLATE_STRING_IF_INVALID:
                        global invalid_var_format_string
                        if invalid_var_format_string is None:
                            invalid_var_format_string = '%s' in
settings.COMPILED_TEMPLATE_STRING_IF_INVALID
                        if invalid_var_format_string:
                            return
settings.COMPILED_TEMPLATE_STRING_IF_INVALID % self.var
                        return
settings.COMPILED_TEMPLATE_STRING_IF_INVALID
                    else:
                        obj =
settings.COMPILED_TEMPLATE_STRING_IF_INVALID
        else:
            obj = self.var

        for func, args in self.filters:
            arg_vals = ""
            i = 0
            for lookup, arg in args:
                if i > 0 :
                    arg_vals = arg_vals + ","
                if not lookup:
                    arg_vals = arg_vals + "\"%s\"" % mark_safe(arg)
                else:
                    arg_vals = arg_vals + "%s" % arg.compile(context)

            if getattr(func, 'needs_autoescape', False):
                new_obj = "%s(%s,autoescape=context.autoescape,%s)" %
(func.__name__,obj,arg_vals)
#                new_obj = func(obj, autoescape=context.autoescape,
*arg_vals)
            else:
                new_obj = "%s(%s,%s)" % (func.__name__,obj,arg_vals)
#                new_obj = func(obj, *arg_vals)
            if getattr(func, 'is_safe', False) and isinstance(obj,
SafeData):
                obj = "mark_safe(%s)" % new_obj
            elif isinstance(obj, EscapeData):
                obj = "mark_for_escaping(%s)" % new_obj
            else:
                obj = new_obj
        return obj

Simple example of used modifications :

from django.template import Parser, FilterExpression

token = 'variable|default:"Default value"|date:"Y-m-d"'
p = Parser('')
fe = FilterExpression(token, p)
fe.compile()

will produce :
'date(default(resolve(context,variable),"Default value"),"Y-m-d")'

Structure of compiled template will consists of 2 parts :
Dependencies part. Section consists of importing modules. Planned that
dependencies should be added by using {% load %} tag for developer-
written tags.
Code part. Section that will consist of internal template functions,
such as «resolve» that will get variable name and resolve it using
current context, and «render» that will consist of compiled template.
E.g. :

# default imports
from django.template.defaulttags import *
from django.template.defaultfilters import *
# importing external modules, section created by using {% load %} tag
from ... import ...

def resolve(context,var_name):
    """ should be looked like method _resolve_lookup in
django.template.Variable """

def render(context):
    """ compiled template will be here """

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to