Forwarding a php array to twig template (Symfony)

user3924331

I am trying to forward (render) my array from php to the twig template and than print everything out in the template. My problem is to access the array in the twig template.

Php array and render function:

    while ( $row = mysqli_fetch_array ( $queryResult ) )
    {
        $tplArray = array (
                array (
                        'id' => $row ['id'] 
                ),
                array (
                        'name' => $row ['name'] 
                ) 
        );

        $tplArray = array (
                'id' => $row ['id'],
                'name' => $row ['name'] 
        );
    }

    return $this->render ( 'work/work.html.twig', array (
            'data' => $tplArray 
    ) );

Trying to access the array in the twig template:

    {% for entry in data %}
        {{ entry.id }}
        {{ entry.name }}
    {% endfor %}

This obviously do not work. How can I get the data (id, name) from my $tplArray and print it out in the template?

raina77ow

Your while loop should look like this:

$tplArray = array(); 
while ( $row = mysqli_fetch_array ( $queryResult ) )
{
    $tplArray[] = array (
         'id' => $row ['id'],
         'name' => $row ['name'] 
    );
}

With this code, you assign an empty array (as a precaution) to $tplArray first. Then, using [] operator, you push a new element - an associative array with 'id' and 'name' attributes - at each step of while loop into this resulting array.

Currently, you reassign a new array as $tplArray value at each step of while instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

symfony php template extends twig template

From Dev

Include function in Twig template Symfony

From Dev

Twig/Symfony form template syntax

From Dev

Image is not displaying in Symfony twig template

From Dev

Symfony, twig template - string concatenation

From Dev

Display array in Twig template

From Dev

Display array in Twig template

From Dev

Getting array keys in Twig? (Symfony)

From Dev

symfony twig array key display

From Dev

Getting array keys in Twig? (Symfony)

From Dev

Using a Twig variable inside a Symfony translation in a template

From Dev

Symfony2 twig mobile template fallback

From Dev

Symfony include navbar html into Twig template

From Dev

Twig Template on Symfony Exception Page not rendering head

From Dev

Missing Twig template in PhpStorm with Symfony Plugin

From Dev

cannot load Twig template (Symfony 2.8.8)

From Dev

Unexpected "javascripts" tag in a twig template in Symfony

From Dev

Symfony2 Twig Conditional Template Name

From Dev

Displaying symfony object values in twig template

From Dev

Template hierarchy at Twig and Symfony2

From Dev

Symfony2 Twig Conditional Template Name

From Dev

Symfony-twig template form theme error

From Dev

Symfony Twig template form redirecting broken

From Dev

Access a php global in twig template

From Dev

Multidimensional array in twig php

From Dev

ContextErrorException and Twig syntax error issue with a twig template - Symfony (FOSUserBundle)

From Dev

Twig (in Symfony) : access template parameters from twig extensions

From Dev

Twig / Symfony 2.3.4: Extending twig template stored in database

From Dev

Symfony: Is there any way to render a twig template using a relative path to the template?

Related Related

HotTag

Archive