Skip to content

Instantly share code, notes, and snippets.

@michaelBenin
Created June 10, 2013 22:18
Show Gist options
  • Select an option

  • Save michaelBenin/5752919 to your computer and use it in GitHub Desktop.

Select an option

Save michaelBenin/5752919 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelBenin revised this gist Jun 10, 2013. 1 changed file with 11 additions and 7 deletions.
    18 changes: 11 additions & 7 deletions mustache.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    # Decorator to register a tag that takes the context
    @register.simple_tag(takes_context=True)
    # Function which takes the django context class and the template string
    def mustache(context, template):
    def mustache(context, template, locale):

    # This can be modified to whatever template directory you want to hold your mustache templates
    # Opens the file for reading as a raw string, cannot use django's get_template or render_to_string methods
    @@ -17,10 +17,14 @@ def mustache(context, template):

    # To retrieve the context passed and not the class info from django, we convert the context to a list
    # After converted to a list, if the context is being passed from base, we want the first item in the list
    # If the context is passed from a block, we want the second item in the list
    if(list(context) == 'dict'):
    context = list(context)[0]
    else:
    context = list(context)[1]
    # If the context is passed from a block, we want the second item in the list, from an include the third

    # In each template load in the custom tag: {% load mustache %}
    context_map = {
    'base': list(context)[0], # {% mustache "_came_from_base_template.html" 'base' %}
    'block': list(context)[1], # {% mustache "_came_from_block.html" 'include' %}
    'include': list(context)[2] # {% mustache "_came_from_include.html" 'include' %}
    }

    # Return the rendered template with pystache
    return pystache.render(template, context)
    return pystache.render(template, context_map[locale])
  2. michaelBenin created this gist Jun 10, 2013.
    26 changes: 26 additions & 0 deletions mustache.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    from django import template
    from django.conf import settings
    import pystache

    # Needed to register a custom template tag
    register = template.Library()

    # Decorator to register a tag that takes the context
    @register.simple_tag(takes_context=True)
    # Function which takes the django context class and the template string
    def mustache(context, template):

    # This can be modified to whatever template directory you want to hold your mustache templates
    # Opens the file for reading as a raw string, cannot use django's get_template or render_to_string methods
    # Because they attempt to render the context and actually return a class which pystache cannot use
    template = open(settings.TEMPLATE_DIRS[0]+"/"+template, 'r').read()

    # To retrieve the context passed and not the class info from django, we convert the context to a list
    # After converted to a list, if the context is being passed from base, we want the first item in the list
    # If the context is passed from a block, we want the second item in the list
    if(list(context) == 'dict'):
    context = list(context)[0]
    else:
    context = list(context)[1]
    # Return the rendered template with pystache
    return pystache.render(template, context)