Symfony2/Twig - Outputting an array of data - symfony

I was wondering the best way to do something. My controller makes a call to a service's function. This function obtains some DOMDocument stuff and I didnt want to pass this whole response to twig. So what I do is this
public function terminalService($terminal_command)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$responseData = $this->commandsService->executeSingleCommand($terminal_command);
$dom->loadXML($responseData);
$elements = $dom->getElementsByTagNameNS('http://www.test.com/ter_0', 'Text');
$elNum = 0;
$flightInfo = array();
while ( $elNum < $elements->length ) {
$flightInfo[] = $elements->item($elNum)->nodeValue;
++$elNum;
}
return $flightInfo;
}
So that takes the DOMDocument, finds all the elements which have the Text namespace and places them into an array element. My controller then receives this array and then passes it to the view
$commandData = $apiService->terminalService($terminal_command);
return $this->render('NickAlertBundle:Page:terminal.html.twig', array(
'data' => $commandData,
));
How can I get each element of the array displayed on a new line? In my view I am currently doing
<div class="col-md-8" id="terminal-window">
{% if data is defined %}
{% for d in data %}
{{ d|nl2br }}
{% endfor %}
{% endif %}
</div>
All that does however is display everything in one long string. funny enough, if I dump data it displays it how I want (but I obviously cant do this in production). So how can I display each array element on its own line?
Update
A dump displays the data how I want it displays e.g.
6 => "3*A$NZ9835 C4 D4 J4 Z4 B7 H7 M7 Y7 LHRLAX 1035 1350 * 777 0E"
7 => " Q7 T7 V7 W7 G7 K7 L7 S7 "
So as you can see line 7 is displayed with a lot of spaces in order to format the output correctly. If I simply place a break at the end of my output, it displays like so
7 #AY4014 F9 A9 P9 J9 C9 D9 I9 Y9 LHRLAX 1215 1530 * 77W 0E
B9 H9 K9 M9 L9 V9 S9 N9 Q9 O9 G9
Is there any way to retain the spaces or duplicate what a dump does?
Thanks

Since you have most of the logic set, I would change the for loop to this:
<div class="col-md-8" id="terminal-window">
{% if data is defined %}
{% for d in data %}
<pre>{{ d }}</pre><br />
{% endfor %}
{% endif %}
</div>

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)

Reverse array in Twig

I'm doing a file explorer with Symfony 2.5 and PHP 5.3
In my Controller I return an array to my view (result of calling scandir(); on a directory)
The array contains "months". (January, February, March...) and I display it in a Bootstrap accordion.
I want to order by descending this array like:
December -> November -> October -> September ...
We can see that {{ for months in month|sort|reverse }} doesnt work here.
How can I do this please?
You need to sort array by index, otherwise it will sort array alphabetically:
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% for month in months|reverse %}
{{ month }}
{% endfor %}
You can use the PHP function array_reverse(). It gives you the array in reverse order. So you don't have to do anything in Twig.
$input = array(1, 2, 3);
$reversed = array_reverse($input);
Of course you have to use this function in your controller.
To order months not alphabetically but by time, see this post.
PHP re-order array of month names
This question has already been answered. You just have to order them like this in your controller and you're done.

Types of SNMPvarbind fields in Scapy

I'm trying to create custom SNMP-trap.
send(IP(dst="10.152.1.81", src="10.152.1.100")/UDP(sport=55555,
dport=3338)/SNMP(version=1 , community= 'test' ,
PDU=SNMPtrapv2(id=14452,
varbindlist=[SNMPvarbind(oid='1.3.6.1.2.1.1.3.0',value=6100)])))
In Wireshark:
...
Value (Integer32): 6100
...
In hex it looks:
02 02 17 d4
Where '02 02' is value type (Integer32).
But i need to change value type to
Value (Timeticks): 6100
Hex:
43 04 17 d4
Yes, i can change it using hex:
e=IP(dst="10.152.1.81", src="10.152.1.100")/UDP(sport=55555, dport=3338)/SNMP(version=1 , community= 'test' , PDU=SNMPtrapv2(id=14452, varbindlist=[SNMPvarbind(oid='1.3.6.1.2.1.1.3.0',value=6100)]))
>>> str(e)
"E\x00\x00E\x00\x01\x00\x00#\x11b\xc3\n\x98\x01d\n\x98\x01Q\xd9\x03\r\n\x001T\xf10'\x02\x01\x01\x04\x04test\xa7\x1c\x02\x028t\x02\x01\x00\x02\x01\x000\x100\x0e\x06\x08+\x06\x01\x02\x01\x01\x03\x00\x02\x02\x17\xd4"
>>> c = "E\x00\x00E\x00\x01\x00\x00#\x11b\xc3\n\x98\x01d\n\x98\x01Q\xd9\x03\r\n\x001T\xf10'\x02\x01\x01\x04\x04test\xa7\x1c\x02\x028t\x02\x01\x00\x02\x01\x000\x100\x0e\x06\x08+\x06\x01\x02\x01\x01\x03\x00\x43\x04\x17\xd4"
>>> send IP(c)
But this method is too difficult when i use 'large' traps.
Whether there is a another method to change this type, or where i can find all possible fields(maybe value_timeticks, value_oid, etc) for SNMPvarbind in Scapy?
Sorry for my google-english.
Thanks.
I think you could use the class ASN1_TIME_TICKS
it should look like this:
varbindlist=[SNMPvarbind(oid=ASN1_OID("1.3.6.1.2.1.1.3.0"),value=ASN1_TIME_TICKS(6100))]
http://fossies.org/dox/scapy-2.2.0/classscapy_1_1asn1_1_1asn1_1_1ASN1__TIME__TICKS.html

Render SQL result in twig file

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 ..

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