1

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 .

1
  • 1
    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. Commented Aug 10, 2017 at 19:52

2 Answers 2

2

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 }}
Sign up to request clarification or add additional context in comments.

Comments

0

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,
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.