【问题标题】:Cannot retrieve object information passed from php to twig无法检索从 php 传递到 twig 的对象信息
【发布时间】:2020-03-23 01:22:27
【问题描述】:

我有一个作为大学项目的课程描述网页,我目前正在该页面上显示有关所有课程批准状态的信息。我正在将存储在数据库中的所有课程检索到 php 中的数组中,然后将该数组传递给我想要遍历课程数组的树枝文件。

但是,twig 似乎无法识别 'courseList' 中的 'course' 变量是什么,因为没有显示任何信息。

树枝

<tbody>
        {% for course in courseList %}
        <tr>
            <th scope="row">{{ loop.index }}</th>
            <td>{{ course.getCourseCode }}</td>
            <td>{{ course.getCourseName(0, course.getCourseId) }}</td>
            <td class="font-weight-bold">{{ course.getCourseApprovalDue }}</td>
            <td>
                <div class="progress">
                    <div class="progress-bar {% if course.getStatus == 0 %} bg-warning
                    {% elseif coruse.getStatus == 1 %} bg-danger {% else %} bg-success {% endif %}" role="progressbar"
                         style="width: 100%" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10">{{ course.getStatus }}
                    </div>
                </div>
            </td>
        </tr>
        {% endfor %}
        </tbody>

PHP

if ($session->has('loggedin')) {

    $courseList = $database->getAllCourses();

    echo $twig->render('courseStatus.twig', array('user' => $session->get('user'), 'loggedIn' => true, 'courseList' => $courseList));
} else {
    echo $twig->render('login.twig');
}

数据库查询和 php 函数工作正常。我可以通过将硬编码的课程对象传递给树枝文件来验证这一点。 coruseList 数组也不为空,因为 twig 文件在渲染时显示 5 行,这与存储在数据库中的课程数量相同。

【问题讨论】:

  • 问题必须出现在getAllCourses() 中,它没有以您认为它返回的格式返回数据。

标签: php twig


【解决方案1】:

从 getAllCourses() 返回的数组不会创建 Course 的新实例,它只是一个包含 Course-data 数组的数组。解决方案是获取数组,并使用返回的信息创建一个包含 Course-objects 的新数组,并将该数组传递给 php-file。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多