-1

I am using twig for the first time and I want to pass a variable to the template, So How I can pass a new template variable? I want to add something like :

{{ Total_project }}

This variable is a return for a function that count the projects and return the total:

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

What shall I do exactly?

Mohdows
  • 61
  • 8
  • You pass the variables when you render the template. See [the docs](https://twig.symfony.com/doc/2.x/api.html#rendering-templates) for more info. – rickdenhaan Feb 17 '18 at 00:21
  • The template is already rendered! – Mohdows Feb 17 '18 at 00:28
  • Call the function _before_ render, and pass the result into twig per the docs @rickdenhaan pointed you at. Also, your function doesn't return anything... – Jonnix Feb 17 '18 at 01:07

1 Answers1

0

Rendering with template :

    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
    ));
}