1
votes

so I have some php variables, some are form an array. I can print them like this

echo $arr->title;
echo $arr->date;
echo $xml;
echo $cover;

I am using twig. I think I need to combine specific php variables into one array(say book) so I can render my twig template with

echo $template->render(['book' => $book]);

then in my twig template be able to use

{{ title }}
{{ date }}
{{ xml }}
{{ cover }}

any tips on how to achieve this would be greatly appreciated .

2
What exactly is the problem with creating the array for your needs? Why can't you fill it out with the variables you have? You asking about variable variables here? Question seems a bit unclear to me.Antwan van Houdt

2 Answers

2
votes

Just create the array for your needs:

$viewData = [
    'book' => [
        'title' => $arr->title,
        'date' => $arr->date,
        'xml' => $xml,
        'cover' => $cover,
    ]
];

echo $template->render($viewData);

Template

{{ book.title }}
{{ book.date }}
{{ book.xml }}
{{ book.cover }}
0
votes

If you want to be able to use the data in your twig template like this:

{{ title }}
{{ date }}
{{ xml }}
{{ cover }}

then you need to pass on the data to the view like that:

echo $template->render([
    'title' => $arr->title,
    'date' => $arr->date,
    'xml' => $xml,
    'cover' => $cover,
]);