python - Can I use a django template to render django templates? -
python - Can I use a django template to render django templates? -
i trying generate website's homepage contains various sections of different types. there's 'about us' section, 'recent projects' section etc. goal leave view logic , template rendering individual apps create project (about, portfolio, etc.). current solution involves constructing django template dynamically , rendering it, so:
in myapp/templatetags/myapp_tags.py:
class="lang-py prettyprint-override">@register.simple_tag def render_sections(sections): template = "" section in sections: if section.type == "about": template += "{% load %}" template += "{% render_about_section %}" elif section.type == "portfolio": template += "{% load portfolio %}" template += "{% render_portfolio_section %}" homecoming render_to_string(template)
in templates/index.html:
class="lang-html prettyprint-override">{% load myapp_tags %} {% block content %} {% render_sections %} {% endblock %}
since i'm constructing template on fly, wondering: why couldn't utilize django's template scheme render django templates? illustration template replace above code be:
class="lang-html prettyprint-override">{% s in sections %} {% load {{s.type}} %} {% render_{{s.type}}_section %}} {% endfor %}
what create work? , there improve ways render list of heterogeneous items?
you can add together template_string_if_invalid = '{{ %s }}'
settings. replace every invalid variable {{ variable_name }}
, stated here.
but has major drawbacks:
it setting, can't alter templates (and switching dynamically evil) it works simple variables tags, not more complex stuff (like{% x in y %}
) not total answer, might help someone.
python django templates
Comments
Post a Comment