Trying to deal with this php framework: https://github.com/daveh/php-mvc Twig is used there. The first time I come across it.

The User model has a getAll method (fetching all users).

In the home page controller (Home) there is an indexAction method, which returns the view (view) View::renderTemplate('Home/index.html');

The most important question: So how do I now display a list of all users in the view?

I can not write php code in index.html (can not be changed to .php, gives an error).

I found out that you can pass variables when creating a view:

 View::render('Home/index.php', [ 'name' => 'Dave' ]); 

But it did not help much. Instead of Dave, I need a method from the model that will return me a list of all users.

I would be extremely grateful for the help!

    1 answer 1

    Oh my God! I figured it out!)

    Had:

    1. Add the use \App\Models\User; Home page controller use \App\Models\User; to access the User model and its methods.
    2. In the same controller in the method of forming the view (View) add a parameter (an array with a list of users, which will be transmitted to the View):

      View::renderTemplate('Home/index.html', [ 'users' => User::getAll() ]);

    3. Now we have an array of users, which can be displayed in View using this construction:

       {% for user in users %} <p>{{ user.name }}</p> {% endfor %} 

    If I wrote something wrong, correct me, please) This is my first day in learning Twig and practice with the php MVC framework)