Twig and pagerfanta get the fieldname and the fieldname.value - doctrine

I'm trying to get the fieldname of a pagerfanta results.
Twig template
{% for post in posts %}
{{ dump(post) }}
{% endfor %}
The dump result is:
Users {#764 ▼
-iduser: 11
-username: "xzvdsfg"
-password: "SHHHHHH"
-lastlogin: DateTime {#691 ▶}
-roles: []
}
i want to get the name of each entry without known it to place in a table like this
<table>
<thead>
<th>{{ fieldname }}</th>
</thead>
<tbody>
<td>{{ fieldname.value }}</td>
</tbody>
</table>
and get this result
<table>
<thead>
<th>username</th>
</thead>
<tbody>
<td>xzvdsfg</td>
</tbody>
</table>
I'm new at Twig templates
Thanks!!!!

You could cast the object to array and iterate over then. You have to create your own twig filter to casting your object to array.
{% for key, value in post|cast_to_array %}
{% endfor %}

Related

how i can sort rows of twig table by using a Stimulus js controller? ican't use knp paginator sort

<div class="table-responsive">
<table class="table align-middle table-nowrap table-check table-striped " id="sortMe">
<thead class="table-light">
<tr>
<th city</th>
<th contry</th>
<th>address</th>
</tr>
</thead>
<tbody>
{% for offer in pagination -%}
<tr>
<td>{{ offer.city }}</td>
<td>{{ offer.contry }}</td>
<td>{{ offer.address }} %</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>

How to concatenate a href in a go html template

I'd really appreciate if somebody could help me out with this. I've been going through similar questions asked but none seem to help with my problem.
I have a User table in a go template. In the right most column I have two buttons to update and delete a user. I want these buttons to point to a url that will update and delete that specific user. So I need to have the user id in the url.
This is the code:
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Date Of Birth</th>
<th>County</th>
<th>Books</th>
<th>Perform Action</th>
</tr>
</thead>
<tbody>
{{ range .Users}}
<tr>
<td>{{ .Id }}</td>
<td>{{ .Name }}</td>
<td>{{ .DateOfBirth }}</td>
<td>{{ .County }}</td>
<td>
{{if .Books}}
{{ range $id, $book := .Books }}
<li>{{ $book.Title }}</li>
{{ end }}
{{else}}
No Books
{{end}}
</td>
{{ $updateUrl := "http://localhost:8080/library/updateuser/" + .Id }}
<td>Update UserDelete User</td>
</tr>
{{ end}}
</tbody>
</table>
In the 5th line up from the bottom you'll see my incorrect attempt at concatenating the href with the id.
You cannot concatenate strings using +. This should work:
<a href="http://localhost:8080/library/deleteuser/{{ .Id }}">

How to display local stored images with the path out of my SQLite database? [duplicate]

This question already has an answer here:
Reference template variable within Jinja expression
(1 answer)
Closed 2 years ago.
I'm writing a little programm in Python, Flask which schould write all whatsapp message informations (using Twilio) in an SQLite Data Base. The storing of the other informations is working fine (ID, Rufnummer, Text, Datum). For the sent images I'm storing the path in the database an the images in static/Images.
<table>
<tr>
<th>ID</th>
<th>Rufnummer</th>
<th>Screenshot</th>
<th>Text</th>
<th>Datum</th>
</tr>
{% for task in tasks %}
<tr>
<td>{{ task.id }}</td>
<td>{{ task.Rufnummer }}</td>
<td>{{ task.Screenshot }}<img src="{{ url_for("static", filename="/Images/{{ task.Screenshot }}") }}" width="30" height="40"/></td>
<td>{{ task.Text }}</td>
<td>{{ task.Datum.date() }}</td>
<td>
Löschen
</td>
</tr>
{% endfor %}
</table>
Most of my programm is from tutorials and copied together! :'D
I think that this part is not right: filename="/Images/{{ task.Screenshot }}"
webapplication of the database
Thank you for your Help!
Best regards,
Mathias
Just use string concatination. Something like:
{{ url_for("static", filename="Images/" + task.Screenshot) }}

how to add data to parameter when using route helper, needed data store inside variable

this a tag use in table, so using table data I want to forward id to new page, here I attach my code
<table class="table">
<thead>
<tr>
<th scope="col">Add ID</th>
</tr>
</thead>
#foreach($PDFS ?? '' as $PDF)
<tr>
<td>{{$PDF->AddID}}</td>
<td>open
</td>
</tr>
#endforeach
<tbody></tbody></table>
this is not working.
but below code is working
<a href="{{ route('ORP_Print_SMID', [app()->getLocale(),'id' => '24']) }}" >find</a>
actual data in this variable is '24'
{{$PDF->AddID}} =24
how I solve this, plz help me
There is no need to use {{ }} again inside {{ }}
Change
<td>open
to
<td>open

Large set of data, DataTables render time awful

I have a view that displays a set of data of about 3000+ records (multiple objects combined into a QuerySet. The result is rendered into a table using DataTables but the render time is terrible - 15 seconds at least.
I dug deeper into DataTables and found deferRender API option together with AJAX thing. As I never used it before I'd like to ask for guidance on how to approach in my particular case. Below are views.py and template with DataTables.
views.py:
def index(request):
clients = Client.objects.all().prefetch_related('loan_set', 'loan_set__case_set')
return render(request, 'app/index.html',
{'clients': clients})
index.html:
<table id="case_list" class="display">
<thead>
<tr>
<td>Imię i nazwisko</td>
<td>Edycja</td>
</tr>
</thead>
<tbody>
{% for c in clients %}
<tr>
<td>{{ c.firstName }} {{ c.lastName }}</td>
{% if c.editLock %}
<td><i class="fas fa-lock fa-lg" style="color:Red" title="W edycji przez: {{ c.lockedBy }}"></i> {{ c.lockedBy }}</td>
{% else %}
<form method="post">
{% csrf_token %}
<td><i class="fas fa-pen-square fa-2x" style="color:green"></i></td>
</form>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('#case_list').DataTable( {
deferRender: true
}
);
});
</script>
Look at the documentation for datatables.
$('#case_list').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "{yourdata_url}"
} );
In your view, you have to send the data in json format. Note that you need to do the search etc with your logic in the view.
I would also look into django-datatable-view package.

Resources