Issue #13 new

Cannot set variables as values

rubinhozzz
created an issue

For example, i want to set the value of the telephone but it accepts just strings. How to do that with a variable without using render_field but the tag? form.telephone.label

    <div class='form-group'>
        <label for='id_telephone' class='col-sm-2 control-label'>{{ form.telephone.label }}:</label>
        <div class='col-sm-10'>
            {{ form.telephone|add_class:'form-control'|attr:"placeholder:form.telephone.label" }}
        </div>
    </div>

Comments (1)

  1. Mikhail Korobov repo owner

    Hi,

    This use case is indeed not supported, good catch.

    But I don't want to add automatic string interpolation for "attr" values - it'd be backwards incompatible and with its own subtle issues (e.g. one don't want to expand variables if value is computed from user input).

    You can add a "placeholder" template filter to your project relatively easy, something like this:

    from django.template import Library
    register = Library()
    from widget_tweaks.templatetags.widget_tweaks import set_attr
    
    @register.filter("placeholder")
    def placeholder(field, data):
        return set_attr(field, 'placeholder:' + data)
    

    If you want, you can submit a PR with such filter (+ a note in docs with your example and a simple test); I think it fits django-widget-tweaks well.

  2. Log in to comment