In one of my of my pages I have a few tables. These tables have the same structure but differ in colors and in the data displayed.
An example table might look like this:
<div class="container reusable-table">
<h4 class="table_title">{{ table_title }}</h4>
{% for item in items %}
<div class="container row">
<div class="container col">
<h4>{{ item.date }}</h4>
</div>
<div class="container col">
<h4>{{ item.time }}</h4>
</div>
<div class="container col">
<h4>{{ item.title }}</h4>
</div>
<div class="container col">
<h4>{{ item.description }}</h4>
</div>
<div class="container col">
<h4>{{ item.price }}</h4>
</div>
</div>
{% endfor %}
</div>
One of my options is to repeat the html and css code for the table each time. This way I risk errors and alot of duplicated code.
From my perspective, I need to find a way to substitute:
- html elements, like
table_title
- data related elements, like
{% for item in items %}
- colors
Based on the comments and the answers below, also on Assign variables to child template in {% include %} tag Django, you can replace html elements and data with the following:
{% include "new_table.html" with table_title="A Title" and items=new_items %}
That leaves the color aspect, or in better terms the css aspect.
I am searching for a way to make this more efficient.
Table Title
` with `{{ table_title }}
` and do `{% include 'my_table.html' with table_title='foo'`. How would you replace `{% for item in items %}`, can you do something like {% for item in {{ items }} %} ? Essentialy how can you pass `items` in the `{% include %}` to be replaceable with a variable ? – IordanouGiannis Feb 26 '18 at 12:55