Jekyll and Liquid - Filling table rows with array - arrays

I'm having some trouble filling table rows with an array.
What I would like to achieve:
The header row and the first column work fine, but I don't get the numbers right, that the first row contains ONLY the first number of the array (i.e. 50), the second row only the second number from the array etc.
My code:
---
exercises: [Burpees, Squats, Pull ups, Push ups]
rounds: 5
reps: [50, 40, 30, 20, 10]
---
<table class="responsive-table">
<thead>
<tr>
<th scope="col">Round</th>
{% if page.exercises %}
{% for exercise in page.exercises %}
<th scope="col">{{ exercise }}</th>
{% endfor %}
{% endif %}
</tr>
</thead>
<tbody>
{% for n in (1..{{page.rounds}}) %}
<tr>
<th scope="row">Round {{ n }}</th>
<!-- THIS PART DOESNT WORK
{% for exercise in page.exercises %}
<td data-title="{{ exercise }}"> {{ page.reps }} </td>
{% endfor %}
-->
</tr>
{% endfor %}
</tbody>
</table>
How can I fill the cells the way in the picture?

exercises: [Burpees, Squats, Pull ups, Push ups]
reps: [50, 40, 30, 20, 10]
---
<table class="responsive-table table">
<thead>
<tr>
<th scope="col">Round</th>
{% for exercise in page.exercises %}
<th scope="col">{{ exercise }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% assign total = 0 %}
{% for rep in page.reps %}
{% assign total = total | plus: rep %}
<tr>
<th scope="row">Round {{ forloop.index }}</th>
{% for exercise in page.exercises %}
<td data-title="{{ exercise }}"> {{ rep }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th scope="row">Sum</th>
{% for exercise in page.exercises %}
<td>{{ total }}</td>
{% endfor %}
</tr>
</tfoot>
</table>

Related

How to add "caption" tag in TableBlock template in StreamField?

I'd like to add <caption> tag for tables created with TableBlock in StreamField, to make the tables more accessible and semantically correct. Right now, the default template doesn't have that tag. I'm not sure how I should be customizing the table.html template (from the default, and I'm not sure how to render the table if I make a custom class for the table block to add captioning.
<caption> must be <table>'s first child, that's why I need to tinker with the template. Anyone has doe this before?
<table>
<caption>Example Caption</caption> <--- Need this here
<thead>
<th>Header col 1</th>
<th>Header col 2</th>
</thead>
<tbody>
<tr>
<td>Cell col 1</td>
<td>Cell col 2</td>
</tr>
</tbody>
</table>
Take a look at the following code.
TableBlock.py
class TableHead(blocks.StructBlock):
table_head_text = blocks.ListBlock(blocks.CharBlock(max_length=120))
class TableRow(blocks.StructBlock):
table_row_text = blocks.ListBlock(blocks.CharBlock(max_length=120))
class TableBlock(blocks.StructBlock):
caption_text = blocks.CharBlock(max_length=120)
table_head = TableHead()
table_rows = blocks.ListBlock(TableRow())
class Meta:
template = 'wagtail_blocks/table.html'
You will notice that I have set the 'template' property in the 'Meta' class, so you will have to create that file too.
table.html
{% load wagtailcore_tags %}
<table>
<caption>{{ value.caption_text }}</caption>
<thead>
{% for header_text in value.table_head.table_head_text %}
<th>{{ header_text }}</th>
{% endfor %}
</thead>
<tbody>
{% for table_row in value.table_rows %}
<tr>
{% for table_row_text in table_row.table_row_text %}
<td>{{ table_row_text }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Finally you can easily use your new TableBlock in any page like this
body = StreamField([
('table', TableBlock()),
])

Editing the model in a ListView

I'm currently making a website for my parent's bakery. And I want to make a custom admin page for them so they can update their products with ease.
All I have so far is a ListView that displays all the products.
This is my model:
class menulist(models.Model):
name = models.CharField(max_length=120)
description = models.CharField(max_length=500)
price = models.DecimalField(decimal_places=1, max_digits=10, default=100.00)
category_choices = (
('breads', 'Breads'),
('cakes', 'Cakes'),
('hotfood', 'Hot Food'),
('porkrolls', 'Pork Rolls'),
('drinks', 'Drinks'),
('MISC', 'Misc'),
)
category = models.CharField(max_length=50, choices=category_choices, default='MISC',)
dateadded = models.DateField(auto_now_add=True)
dateupdated = models.DateField(auto_now=True)
img = models.ImageField(upload_to='products/', default='products/blank.jpg')
def __str__(self):
return self.name
The View:
class ProductAdminView(ListView):
template_name = 'menulistapp/product_admin.html'
queryset = menulist.objects.all()
The template:
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<table class="table table-striped table-hover ">
<thead class="thead-dark">
<tr>
<th style="width: 15%"scope="col"></th>
<th style="width: 55%" scope="col">Product Name</th>
<th scope="col">Category</th>
<th scope="col">Price</th>
<th scope="col">Date Added</th>
<th scope="col">Last Updated</th>
</tr>
</thead>
<tbody>
{% for obj in object_list %}
<tr>
<td class="align-middle"><img src="{{ obj.img.url }}" class="img-fluid"></td>
<td class="align-middle">{{ obj.name }}</td>
<td class="align-middle">{{ obj.get_category_display }}</td>
<td class="align-middle">${{ obj.price }}</td>
<td class="align-middle">{{ obj.dateadded }}</td>
<td class="align-middle">{{ obj.dateupdated }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
How do I separate the items into their own category tables?
And how do I make it so that each cell is editable with their own CharField, and there's a "SAVE" button at the bottom of each table?
EG:
You have to use the concept of forms to attain that
For more information, go through https://docs.djangoproject.com/en/2.1/topics/forms/

Twig loop in assoc array

i have array like
Array (
[data] => Array (
[0] => tratata
[1] => blabla
)
[data_key] => Array (
[0] => the first key
[1] => the second one
)
)
And i tried to display it this way:
{% for key, value in L10_DATA %}
<tr>
<th>
{{ value.data_key }}
</th>
<td>
{{ value.data }}
</td>
</tr>
{% endfor %}
But it doesnt work :c Can you help me, please?
I need to get that king of table, where the first column is data_key and the second one - data:
Because your data is divided into 2 (sub)arrays, you will need to use the index of the first array, to access the data of the second
<table>
<tr>
<th>Key</th><th>Data</th>
</tr>
{% for index, key in L10_DATA.data_key %}
<tr>
<td>{{ key }}</td><td>{{ L10_DATA.data[index] }}</td>
<tr>
{% endfor %}
</table>
demo

iterating over length of string variable jinja2

I'm trying to iterate over the length of a string variable in jinja2.
what I have is:
<table>
<tbody>
{% for i in range(string1|length) %}
<tr>
<td>
test
</td>
</tr>
{% endfor %}
</tbody>
</table>
but when I run the application with flask literally nothing is displayed.
what am I doing wrong?

Proper way to fill an html array using django/python

I encounter a small issue about filling my html array in a django template.
I wanted to print one form to one row. Instead of one row to all forms ( that's what i get with the sample code just right there ) :
<thead> <!-- cétait td à la place de th -->
<tr>
{% for entry in headers %}
<th>
{{ entry }}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{%for entry in entries%}
{%for cle, valeur in entry.all_form_entry_json%}
{%for key, valor in headersLoop%}
{%if key == cle%}
<td> {{valeur}}</td>
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
</tr>
</tbody>
The result is such as follow :
You can notice that all the datas are printed to the right corner and go beyond the array limit.
How can i have a result like this ?
Is this possible to do it only with html ? Or should i create a js or css file and import it to this template.html ?
Thank you for your answers !
I think you just wrong place for the html <tr> tag,
the tag <tr> should inside {% for loop..
<thead>
<tr>
{% for entry in headers %}
<th>{{ entry }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for entry in entries %}
<tr>
{% for cle, valeur in entry.all_form_entry_json %}
{% for key, valor in headersLoop %}
{% if key == cle %}
<td>{{ valeur }}</td>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
</tbody>

Resources