Mako templates using Django template tags

2019-01-25 17:22发布

问题:

Our Django site is built using Mako templates. We want to use a third party project called django-socialregistration, but its template tags use Django's templates. If we used Django templates we could just

{% load facebook_tags %}
{% facebook_button %}
{% facebook_js %}

How can I do the same thing in Mako? You can inline strait up python in Mako, but I haven't figured out how to do it that way either.

Final Fix

<%! from django.template import Template, Context %>
<% tpl = "{% load facebook_tags %}{% facebook_button %}{% facebook_js %}" %>
${Template(tpl).render(Context(dict_=dict(request=request)))}

回答1:

I've hardly used Mako, but if you can include arbitrary Python code, you could always inline the template rendering function there.

<%
    tpl = """{% load facebook_tags %}{% facebook_button %}{% facebook_js %}"""
    from django.template import Template, Context
    t = Template(tpl)
    t.render(Context())
%>