1 year ago

#377772

test-img

itajackass

approach to do not use PHP function inside twig

i've a situation where my php controller return an array from database table "messages" (db has no INNER JOIN with table users!):

$messages = get_messages($filters);

var_dump($messages);

//output var_dump() :
// array(
//   0 => array(
//      'message' => 'text sample',
//      'userID' => 1
//     )
//   1 => array(
//      'message' => 'other text sample',
//      'userID' => 5
//     )
// )

now inside TWIG i use a loop like this:

{% for mex in messages %}
Message: {{ mex.message }}
From user: {{ mex.userID }}
{% endfor %}

My question is: what's the best method to print "name of a user" instead their ID without create a php call function inside the twig loop? example i don't want to use:

{% for mex in messages %}
Message: {{ mex.message }}
From user: {{ php_function_to_retrieve_name_of_user_from_id(mex.userID) }}
{% endfor %}

Of course i can create an inner join from messages table and users table... but i'd like to know if is there a "universal" approach when inside a twig loop i need to retrieve info from others tables in database.... thanks

php

twig

0 Answers

Your Answer

Accepted video resources