# DRY in Template

**URL:** <https://forum.djangoproject.com/t/dry-in-template/15526>\
**Category:** Templates & Frontend\
**Created:** [August 21, 2022, 11:17am UTC](https://forum.djangoproject.com/t/dry-in-template/15526 "2022-08-21T11:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [August 21, 2022, 11:17am UTC](https://forum.djangoproject.com/t/dry-in-template/15526/1 "2022-08-21T11:17:41Z")

</div>

Is there a better way to code this in a template ?  
(I would prefer the classnames are set in the template and not in views.py)

This is within a `{% for field in form %}`

```auto
{% if field.field.widget.input_type != "checkbox" %}
    {% with class="flex-1 block w-full focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none rounded-r-md sm:text-sm border-gray-300" %}
        {% render_field field class=class %}
    {% endwith %}
{% else %}
    {% with class="block focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none sm:text-sm" %}
        {% render_field field class=class %}
    {% endwith %}
{% endif %}

```

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [August 23, 2022, 9:26am UTC](https://forum.djangoproject.com/t/dry-in-template/15526/2 "2022-08-23T09:26:51Z")

</div>

you could do a template tag?

```auto
{% use my fieldtag %}
{% fieldtag field=field %}

```

and somewhere in a `templatetags` folder, `fieldtag.py`:

```auto
from django import template

register = template.Library()

@register.inclusion_tag('fieldtag/index.html', name='fieldtag', takes_context=True)
def fieldtag(context, field=None):
    if not field:
        return {}
    
    # tests here to determine the css classes to apply

    # my god what a long classname :)
    return {
        'class_name': 'flex-1 block w-full focus:ring-indigo-500 focus:border-indigo-500 min-w-0 rounded-none rounded-r-md sm:text-sm border-gray-300'
    }

```

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [August 23, 2022, 9:40am UTC](https://forum.djangoproject.com/t/dry-in-template/15526/3 "2022-08-23T09:40:27Z")

</div>

Oh, ok I didn’t get into template tags till now. Thanks a lot.

> my god what a long classname 🙂

Welcome to the world of TailWindCSS !
