Render SQL result in twig file - symfony

How can i render result of following query in twig ?
$q = $em->createQuery('
SELECT mark, student.studentName FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getArrayResult();
Table contents are
Table tbl_mark_details || tbl_student_details
---------------------------------------------------------------
id | student_id | exam_id | score || id | student name
1 |1 | 1 |10 || 1 | Student 1
2 |2 | 1 |5 || 2 | Student 2
3 |2 | 2 |25 ||
I tried the following code, but no student name for the third row
{% set i=0 %}
{% for mark in marks %}
{% if i%2 == 0 %}
<tr>
<td>{{ mark.score }}</td>
<td>{{ mark.examId }}</td>
{% else %}
<td>{{ mark.studentName }}</td>
</tr>
{% endif %}
{% set i = i+1 %}
{% endfor %}
Thanks
Vishnu V

Your should try like this :
$q = $em->createQuery('
SELECT mark, student FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getResult();
and your twig is all right ..

Related

Is there an configuration to limit the numbers of page numbers in Pagerfanta pagination?

I have a pagination with Pagerfanta. I want to limit page numbers to 5, since on mobile version pagination is too large. I am using default view 'twitter_bootstrap_translated'.
{% if articles.haveToPaginate %}
<div class="pagination-class">
{{ pagerfanta(articles, 'twitter_bootstrap_translated', {routeName: 'search_result_paginated', routeParams: app.request.query.all}) }}
</div>
{% endif %}
How I can limit the page numbers/links?
Currently: < Prev | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ... 101 | Next >
I need pagination like this:
Currently: < Prev | 1 | 2 | 3 | ... 101 | Next >
Please have a look might be useful:
<?php
use Pagerfanta\View\TwitterBootstrapView;
$view = new TwitterBootstrapView();
$options = array('proximity' => 3);
$html = $view->render($pagerfanta, $routeGenerator, $options);
Options (default):
proximity (3)
prev_message (← Previous)
prev_disabled_href ()
next_message (Next →)
next_disabled_href ()
dots_message (…)
dots_href ()
css_container_class (pagination)
css_prev_class (prev)
css_next_class (next)
css_disabled_class (disabled)
css_dots_class (disabled)
css_active_class (active)

Round up a variable with Shopify Liquid

I wish to assign a dummy variable to a math value so I can then take the ceiling.
My current code is:
{% if variant.compare_at_price > variant.price %}
SAVE {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
{% endif %}
Output is SAVE 20% (for example, but if it's 19.99 it'll be 19% rather than 20%)
But I want to call:
x= {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
then take {{ x | ceil }}
How do I assign x?
By using the assign variable tag:
{% assign x = variant.compare_at_price | minus:variant.price | times:100.0 | divided_by:variant.compare_at_price %}
{% assign x = x | ceil %}
SAVE {{x}}%

How to build vertical data form in Symfony2, where fields are key-value rows?

Lets say I got some kind of metadata table.
| ID | Key | Value | Data Type | Label |
|----|------|--------|-----------|-------|
| 1 | key1 | value1 | text | ... |
| 2 | key2 | value2 | boolean | ... |
| 3 | key3 | value3 | file | ... |
| 4 | ... | ... | ... | ... |
And it should be displayd as form using Symfony2 Form component and Doctrine2 ORM:
<form>
<label>{{ label1 }}</label><input type="text" name="{{ key1 }}" value="{{ value1 }}">
<label>{{ label2 }}</label><input type="checkbox" name="{{ key2 }}" value="1">
<label>{{ label3 }}</label><input type="file" name="{{ key3 }}" value="{{ value3 }}">
</form>
That is enough to get started :-)
{% for x in xy %}
<label>{{ x.label }}</label>
<input type="
{% if x.datatype == "boolean" %}
checkbox
{% else %}
{{ x.datatype }}
{% endif %}
" name="{{ x.key }}" value="{{ x.value }}">
{% endfor %}

Symfony - Twig - dateTime HH:MM:SS.MMM in SS.MMM?

How to convert a datetime HH:MM:SS:mmm in SS.mmm with a filter in Twig please ( `` )?
exemple :
00:01:30.600 => 90.600
I tried that {{ Object.time | date("s") }} but it doesn't work ...
thanks !
As #john Smith told in his comment (unfortunately I can't +1 but he deserves it), you can use:
{{ Object.time | date("s.u") }}
To change microseconds to miliseconds, you have 2 choices:
Round to 3 digits:
{{ Object.time | date("s.u") | round(3) }}
Slice the 3 last chars:
{{ Object.time | date("s.u") | slice(0, -3) }}

how make addition from 2 variable twig?

Anyone knows how to addition two variable in twig I want to do something like:
{{ var1 }} + {{ var2 }}
Just do it inside the {{ }}
{{ var1 + var2 }}
If you want to assign it to some other variable:
{% set foo = var1 + var2 %}
{{ foo }}
Declaration :
{% set counter = 0 %}
Doing the addition :
{% set counter = counter + 1 %}
For displaying in twig template :
{{ counter }}

Resources