Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Template - Stop processing template

Tags:

python

django

I am looking for way to clean up a template in django. A simple solution would be to break this up into multiple templates, but we do not want to do that.

We basically have the following

{%if data.some_state %}
    Display some markup
{% else %}
   {%if data.some_state_2 %}
       State 2 different html view 
   {% else %}
        {%if data.process_data %}
           Display some list of data
        {% else %}
           No Data to display!
       {% endif %} <!-- if data.process_data-->
   {% endif %} <!-- if data.some_state_2 -->
{% endif %} <!-- if data.some_state -->

So that is extremely confusing and hard to read. If I could do this in a "function" i would use if/else if or returns.

Is there a way in template language to do something like (stop_processing_template would tell the template we are done... ):

{%if data.some_state %}
    Display some markup
{% endif %}
{% django_stop_processing_template %}


{%if data.some_state_2 %}
       State 2 different view
{% endif %}
{% django_stop_processing_template %}

{%if data.process_data %}
           Display some list of data
{% endif %}
{% django_stop_processing_template %}

 No data provided !
like image 388
Nix Avatar asked Dec 28 '22 09:12

Nix


1 Answers

I am not sure what your stop processing template logic would do; but a cleaner way to do your logic would be to write a custom tag that takes your arguments and then returns only the HTML relevant to your variables. This way you remove the if/else loops and instead replace all that with a simple {% do_stuff %} tag.

Edit

This is a very simple implementation to give you some idea on how the logic would go.

First, you create templates for each variation and store them somewhere django can find them.

Then, a simple tag that renders the exact template you want (this is non tested, psuedo):

from django import template
from django.db.models import get_model
register = template.Library()

class ProcessData(template.Node):
    def __init__(self, var_name):
        self.obj = get_model(*var_name.split('.'))
    def render(self, context):
        if self.obj.some_state:
           t = template.loader.get_template('some_markup_template.html')
           result = 'something'
        else:
           if self.obj.some_state_2:
              t = template.loader.get_template('some_different_html_view.html')
              result = 'something'
       else:
          if self.obj.process_data:
             t = template.loader.get_template('some_list_data.html')
             result = 'something'
          else:
             t = template.loader.get_template('no_data.html')
             result = 'something'
        return t.render(Context({'result': result}, autoescape=context.autoescape))

@register.tag
def process_data(parser, token):
    try:
        tag_name, arg = token.contents.split(None, 1)
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires arguments" % token.contents.split()[0])
    return ProcessData(arg)

Finally, in your template:

{% load my_tags %}
{% process_data data.mymodel %}
like image 159
Burhan Khalid Avatar answered Dec 31 '22 14:12

Burhan Khalid