Skip to content

Instantly share code, notes, and snippets.

@starsinmypockets
Last active December 11, 2015 06:08
Show Gist options
  • Save starsinmypockets/4556934 to your computer and use it in GitHub Desktop.
Save starsinmypockets/4556934 to your computer and use it in GitHub Desktop.
Pass context variables to template tags.
from django import template
import main.models # @@TODO this might be a reason to put frame components in a separate file some day
import logging
logger = logging.getLogger(__name__)
register = template.Library()
@register.tag(name="getradiomagic")
def getradiomagic(parser, token):
tag, category = token.split_contents()
return MagicNode(category)
class MagicNode(template.Node):
def __init__(self, category):
self.new_category = template.Variable(category)
def render(self, context):
return self.new_category.resolve(context)
{% load getradiomagic %}
{% block magicradio %}
{% if not nolabel %}
<label for="{{ field.id }}" {% if field.required %}class="required"{% endif %}> {{ field.label }} </label>
{% endif %}
{% for option_value, option_label in field.field.choices %}
<input type="radio" name="{{ option_value }}" value=""{% if field.value == option_value|safe %}selected="selected"{% endif %}/>
<label>{{ option_label }}</label>
<span>{% getradiomagic field.name %}</span>
{% endfor %}
{{ field.help_text }}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment