#Passing a lot of data to a template

1 messages · Page 1 of 1 (latest)

short yew
#

Kind of looking for advice, as I feel there has to be a better way to do what I'm trying to do.

I have reusable form elements that must use a certain styling, name convention, etc, and then boolean options for things like 'Does this field have a hint/tooltip/X/Y/Z'. It's resulted in an unwiedly template that I basically have to use like this to keep readable.

{% with (long string of assignments %}
   {% with (long string of assignments %}
          {% include 'my-awesome-field.html' %}
   {% endwith %}
{% endwith %}

I feel like there must be some more Django/Pythonic way of doing this. Any thoughts?

limpid ether
#

if it's possible to post the whole snippet, it might help me get a better understanding of your problem.

#

I kind of understand, but not fully

short yew
#

I can't post the exact snippet, but I could certainly give you an idea.

#

Take a checkbox. A standard form element for the organization needs

  • A fieldset wrapper
  • A hidden form error in a specific style to render an error, if one is there
  • A wrapping div for the control itself
  • A label that may or may not be there, and may or may not be inline
  • Depending on the exact style of the checkbox, it may or may not be inline, small or big - three separate classes

Something like...

<fieldset id='fieldset-{{id}}'>
     {{if error}}
      <span class='...'>{{error}}
     {{error}}
     <div class="orgcheckboxes {%if small%} orgcheckboxes-small{%endif%}"
       {%if label%}
       <label for='{{id}}'>{{label}}</label>
       {%endif%}
       <input type='checkbox' class='orgcheckbox-item {%if inline%}d-inline{%endif%}' {%if value|default:None %}value={{value}}{%endif%} name={{field_name}}/>
      </div>
</fieldset>

Turns into

{% with id=whatever name=whatever value=whatever label=whatever %}
{% with inline=whatever error=whatever  %}  
     {% include 'my-awesome-checkbox.html' %}
{% endwith %}
{% endwith %}
#

And that's a small one. Ideally I'm trying to create reusable templates that cover all of the options in our design pattern, and I feel like there has to be some better way of doing that

limpid ether
#

are all of these values coming from a form field? Could you instead just pass field to the template instead of each piece individually? Then you can access them like field.label ...

short yew
#

Admittedly I have thought about creating a custom widget