from django import template
from django.forms import Field

register = template.Library()

class PlaceholderForm(template.Node):
    def __init__(self, form_field):
        self.form_field =  form_field
    
    def render(self, context):
        html = self.form_field.resolve(context)
        label = html.label
        return html.as_widget(attrs={'placeholder':label})


@register.tag
def form_placeholder(parser, token):
    try:
        tag_name, form_field_string = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('{0} tag requires a single argument'.format( token.contents.split()[0]))
    form_field = parser.compile_filter(form_field_string)
    return PlaceholderForm(form_field)