如何使用 PHP slim 在 twig 中呈现数据库中的数据



我试图将数据从我的数据库渲染到控制器中的树枝中。有点像 mvc 结构,但没有模型。

现在它只是渲染

我的待办事项

我很惊讶纤薄的文档不包含此类说明

任何建议,提前感谢。

TodosController.php

<?php

namespace AppControllers;
use SlimHttpRequest;
use SlimHttpResponse;
class TodosController extends BaseController
{
    public function index($request, $response)
    {
    }

    public function getTodos($request, $response, $args)
    {
        $sth = $this->db->prepare("SELECT * FROM tasks ORDER BY task");
        $sth->execute();
        $todos = $sth->fetchAll();
        return $this->c->view->render($response, 'todos.twig', $todos);


    }
}

Todos.twig

{% extends "templates/layout.html" %}
{% block content %}
<h1>My Todos</h1>
<ul>
  {% for task in todos %}
        <li><span>{{ task.id}}</span> {{ task.task}}</li>
    {% endfor %}
</ul>
{% endblock %}
你需要

把你的todos放到数组中

$this->c->view->render($response, 'todos.twig', ['todos' => $todos]);

最新更新