如何将php变量传递给twig模板

时间:2018-02-17 00:19:07

标签: php templates twig templating

我第一次使用twig并且我想将变量传递给模板,那么我如何传递新的模板变量? 我想添加类似的内容:

{{ Total_project }}

此变量是对项目进行计数并返回总计的函数的返回值:

public function total_projects(){
        if ($result = mysqli_query($conn, "SELECT project_id as total_project FROM Projects")) {
            $row_cnt = mysqli_num_rows($result);
            }
    }

我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用模板渲染:

    public function total_projects(){
    if ($result = mysqli_query($conn, "SELECT project_id as total_project FROM Projects")) {
        $row_cnt = mysqli_num_rows($result);
        $total_project = $result['total_project'];
    }

    return $this->render('index.html.twig', array(
        'Total_project' => $total_project
    ));
}