I Have a table with this columns:
login ,couleur1,parties,gagnees
I would like to print an array exactly like this one:
In Silex I get all data in an array
$app->get('/userlist', function(Application $app) {
$recup= $app['db']->executeQuery('SELECT * FROM users');
$results = $recup->fetchAll();
return $app['twig']->render('example.twig', array('users' => $results));
});
$app->run();
[enter image description here][2]?>
In Twig I tried to align them, but I can't get it like in the photo.
{% for row in users %}
<ul style="list-style: none;">
<li style="float:left; margin-right:30px" >{{ row.login }}</li>
<li style="float:left; margin-right:30px">{{row.parties}}</li>
<li style="float:left; margin-right:30px">{{row.couleur1}}</li>
<li style="float:both">{{row.couleur2}}</li>
</ul>
{% endfor %}
I would make that without separating CSS and Twig.
It will be something like this:
<table>
<tr>
<td>Joueur</td>
<td>Parties</td>
<td>Gagness</td>
<td>Colueur Preferee</td>
</tr>
{% for row in users %}
<tr>
<td>{{ row.login }}</td>
<td>{{row.parties}}</td>
<td>{{row.couleur1}}</td>
<td>{{row.couleur2}}</td>
</tr>
{% endfor %}
</table>
and apply some CSS for colors and looks.
Related
I am working on a webapplication but ive ran into a hiccup, I have gathered data from my database and displayed the records into a datatable, my problem is that I can not just have one table the client wants me to divide the table into multiple based on what interger is in a column(omloopNummer).
The best ive gotten so far is with the following code it wil split the table up into lets say 4 tables if the interger in the database wil go 1,2,3,4 but it wil only show 1 record in each table while it should be more.
If there is any other simple solution to this instead of using twig I would be happy to try it.
<div id="index" class="table-responsive">
{% set i = 1 %}
{% for duel in duels %}
{% if duel.omloopNummer == i %}
{% set i = i + 1 %}
<table id="" class="table display-" style="width: 90%; margin-top: 30px">
<thead>
<tr>
<th>Omloop</th>
<th>Team 1</th>
<th>vs</th>
<th>Team 2</th>
<th>Acties</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ duel.omloopNummer }}</td>
<td>{{ duel.team1 }}</td>
<td>vs</td>
<td>{{ duel.team2 }}</td>
<td>{{ include('duel/_delete_form.html.twig') }}</td>
</tr>
</tbody>
</table>
{% endif %}
{% endfor %}
You could use PHP to group Duels by omloopNummer in the controller before passing to twig.
$duels = $duelRepository->findAll();
$sorted = [];
foreach($duels as $duel){
$sorted[$duel->getOmloopNummer()][] = $duel;
}
return $this->render('template.html.twig', [
'duel_groups' => $sorted,
]);
Then in twig, loop through the sorted array building a table for each non-empty group and inside each tbody loop through the group.
{% for group in duel_groups %}
...
{% if group is not empty %}
<table>
...
<tbody>
{% for duel in group %}
<tr>...</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
...
{% endfor %}
I have satellite images stored in a mysql database. The table has attributes latitude,longitude. I want to send them to the twig and display as a map, my php controller looks like this.
public function highlightAction()
{
$highlighted=$this->getDoctrine()
->getRepository('AppBundle:satelliteImage')
->findAll();
$images = array();
foreach ($highlighted as $key => $high) {
$images[$key] = base64_encode(stream_get_contents($high->getImage()));
}
return $this->render('satelliteImages/highlighted.html.twig',array(
'highlighted' => $highlighted,
'images' => $images
));
}
My twig code is this:
<tbody>
{% for key,high in highlighted %}
<tr>
<img alt="Embedded Image" src="data:image/png;base64,{{ images[key] }}" />
</tr>
{% endfor %}
</tbody>
I am displaying the images as a vertical array. Any suggestions, I might need to display them as a map.
A two dimensional array in twig?
You can use something like this:
<table>
<tbody>
{% for key in 0..2 %}
<tr>
{% for key in 0..3 %}
<td>
<img alt="Embedded Image" src="data:image/png;base64,
{{ images[loop.parent.key*4 + key] }}" />
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Read the Twig for documentation.
I am new to symfony2 I'm trying to create a search function and got this
error 'Method "ediTransaction" for object "Matrix\MatrixEdiBundle\Entity\Edi997SegmentInError" does not exist in MatrixEdiBundle:Matrix:rejectedTrans.html.twig at line 58'
is it what problem is it? Anyone?
Repository
class Edi997DetailsRepository extends EntityRepository{
public function getDetails($gsNumber, $senderId, $receiverId, $page = 1, $limit = 5 ){
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT partial a.{id, ak301, ak401},
partial b.{errorCode, condition},
partial c.{edi997DetailId, errorCodeId, noOfTrans},
partial d.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName}
FROM MatrixEdiBundle:Edi997SegmentInError a
JOIN a.ediAk403ErrorCodes b
JOIN a.edi997Details c
JOIN c.ediTransaction d
WHERE d.gsNumber LIKE :gsNumber
AND d.senderId LIKE :senderId
AND d.receiverId LIKE :receiverId
AND c.errorCodeId != 1
AND d.flag = 1')
->setParameter('gsNumber', "%$gsNumber%")
->setParameter('senderId', "%$senderId%")
->setParameter('receiverId', "%$receiverId%")
->setFirstResult(($page-1)*$limit)
->setMaxResults($limit);
$paginator = new Paginator($query, $fetchJoinCollection = false );
$paginator->setUseOutputWalkers(false);
return $paginator;
}
}
Twig :
{% if transaction != null %}
{% for trans in transaction %}
<tr>
<td style="width: 11%;">
{{ render(controller('MatrixEdiBundle:Matrix:getTradingPartnerName', {'timexID' : trans.ediTransaction.receiverId, 'customerID' : trans.ediTransaction.senderId})) }}
</td>
{%
set result=render(controller('MatrixEdiBundle:Matrix:getFile', {'fileName' : trans.ediTransaction.fileName, 'senderId': trans.ediTransaction.receiverId , 'receiverId' : trans.ediTransaction.senderId }))|split('+', 4)
%}
<td style="width: 10%;">{{ result[0] }}</td>
<td style="width: 40%;">{{ result[3] }}<br><br>
{% if trans.errorCodeId == 2 %}
<span style="background-color: yellow;">Accepted but <br/>errors were <br/>noted</span><br/>
{% elseif trans.errorCodeId == 3 %}
<span style="background-color: yellow;">Partially<br/> Accepted</span><br/><br/>
{{ trans.acceptedTrans }} Accepted,<br/>
{{ trans.noOfTrans - trans.acceptedTrans }} Rejected
{% else %}
<span style="background-color: yellow;">Rejected</span>
{% endif %}
<br/>
{%
set error=render(controller('MatrixEdiBundle:Matrix:getError', {'id': trans.edi997DetailId }))|split('+')
%}
<span>
<p style="white-space: normal;">
{% if error[0] != "0" %}
{{ error[0] }}{{ "%02d"|format(error[1]) }}<br/>
{{ error[2] }}
{% else %}
Errors were not specified in 997 file
{% endif %}
</p>
</span>
</td>
<td style="width: 10%;">{{ result[2] }}</td>
<td style="width: 7%;">{{ result[1] }}</td>
<td style="width: 5%;">{{ trans.noOfTrans }}</td>
<td style="width: 7%;">{{ trans.ediTransaction.receiverId }}</td>
<td style="width: 8%;">{{ trans.ediTransaction.senderId }}</td>
<td>
<center><i class="fa fa-trash o" style="color:#1975A3;"></i></center>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="15" class="alignment result"> No Result Found </td>
</tr>
{% endif %}
The answer is in the error.
In your Edi997SegmentInError entity object, there is no ediTransaction method.
Probable causes:
The method doesn't exist in any object
The method exists in an
object, just not the one you're accessing
Looking at your repository method, that seems to be in your Edi997Details entity.
Presumably you'd access it via {{trans.edi997Details.ediTransaction.receiverId}} - but I'm really guessing there - you have to follow your object hierarchy correctly in twig. If you're not sure exactly which object you're accessing, try using {{dump(trans)}} to see what you have.
I'm using Twig as template framework for my PHP web application.
I would like to know if there is a fast way to get many element in a foreach block.
This is my data:
users=>[
["name"=>"User1"],
["name"=>"User2"],
["name"=>"User3"],
["name"=>"User4"],
["name"=>"User5"],
["name"=>"User6"]
]
This would be a standard loop (each item):
<ul>
{% for user in users %}
<li>{{ user.name }}</li>
{% endfor %}
</ul>
But this is what I need in block of n elements (in this example n=3)
<ul>
<li>User1</li>
<li>User2</li>
<li>User3</li>
</ul>
<ul>
<li>User4</li>
<li>User5</li>
<li>User6</li>
</ul>
Exists a fast way to do this in Twig or should I prepare the data in a different way with a one more subarray layer?
Looks like you need to use batch filter:
{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
<table>
{% for row in items|batch(3, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
It will render:
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>No item</td>
<td>No item</td>
</tr>
</table>
Source: Twig Documentation
{% for item in items %}
<tr>
<td >
{{item.generated_on| date('d M, Y')| default("")}}
</td>
<td>
{{item.number}}
</td>
<td>
<ul>
{% for detail in item.details %}
<li>
{{detail.label}}
</li>
{% endfor %}
</ul>
</td>
<tr>
i want to change the color of the details.label field and its style also.
details is a object with consist of label and some other fields.
thanks in advance
{% for detail in item.details %}
<li>
<span class="colored-label"> {{detail.label}} </span>
</li>
{% endfor %}
And then give style to colored-label class in css.
.colored-label {
color: #ccc;
/* other styles */
}
Twig isi Beck End template, it barely have no relation with css. Things that you can do to styling the html is just view source of your html on browser and create css file according to it.
or, if you are in control to change detail.label, you can change the html structure of it.