简体   繁体   中英

How to render a view in php and twig?

I'm creating a webpage where students and teachers log in, teachers create levels and students can do that level.

I explain, I am using php with twig and I want to render a view passing parameters in a function. I've created a budle called Professors where I have the directory Controllers, Models and Templates.

In templates I have professors.html, where I show some information and a button to create a level for students, and also I have crearNivell.html, where the teacher will be able to create the level. When I'm in the page views professors my URL is this one:

本地主机/PROJECTE2/教授

When I click the button "CREATE LEVEL" I want my URL to be like this:

本地主机/PROJECTE2/教授/crearNivell

Instead I get this URL, and this one returns me an error.

在此处输入图片说明

In Controllers/ProfessorsController.php I have that code:

class ProfessorController extends Controller{

public function process($params)
{
    /*var_dump($params);
    die();*/

    if(empty($params[0])){

        $this->getProfessor(); //Here I return the view professor

    }elseif(isset($params[0]) && $params[0] == "crearNivell"){

        $this->twig = "crearNivell.html";
    }

}

public function getProfessor(){

    $this->twig = "professor.html";
}

}

Can someone help me with my code?

When I use var_dump() I get this:

在此处输入图片说明

and it should be like that:

在此处输入图片说明

I think what you're looking for is the API of Twig .

More specifically, you need the line below to render a template passing some parameters in array:

echo $template->render(['the' => 'variables', 'go' => 'here']);

If you use PHP the fastest way to "print" some content to the browser is echo , so don't hesitate to use it.

I found the answer to the questions a few days ago. I was complicating myself, it's just like this:

First you create the views, in Templates folder.

The $params are refered to the URL. For example:

alumne is the bundle

在此处输入图片说明

AlumneController.php

Then in AlumneController.php put the name of the views without the .html and you show it using $this->twig = "name_Of_The_View.html";

<?php
 class AlumneController extends Controller{

    public function process($params)
    {
        /*var_dump($params);
        die();*/

        if(empty($params[0])){

            $this->getAlumne();
            /*echo $usuari = $_SESSION["username"];*/

        }else if(isset($params[0]) && $params[0] == "instruccions"){

            $this->twig = "instruccions.html";
            /*echo $usuari = $_SESSION["username"];*/

        }else if(isset($params[0]) && $params[0] == "resultats"){

            $this->twig = "resultats.html";
            /*echo $usuari = $_SESSION["username"];*/

        }
    }

    public function getAlumne(){

        $this->twig = "alumne.html";
    }
 }

alumne.html

And in the button or link you are using to go to the page you write the path in <a href="bundle/view_name">Instruccions</a>

<a class="mdl-navigation__link" href="alumne/instruccions">Instruccions</a>
<a class="mdl-navigation__link" href="alumne/resultats">Resultats</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM