如何在 php 和 twig 中渲染视图?

我正在创建一个网页,学生和教师可以在其中登录,教师创建关卡,学生可以完成该关卡。

我解释一下,我正在使用 php 和 twig,我想渲染一个在函数中传递参数的视图。我创建了一个名为 Professors 的 budle,其中有目录 Controllers、Models 和 Templates。

在模板中,我有professionals.html,我在其中显示一些信息和一个按钮来为学生创建关卡,我还有crearNivell.html,老师可以在其中创建关卡。当我在页面浏览教授中时,我的 URL 是这个:

http://img4.mukewang.com/629072300001513c04630146.jpg

当我单击“创建级别”按钮时,我希望我的 URL 如下所示:

http://img.mukewang.com/629072380001a14904290091.jpg

相反,我得到了这个 URL,这个 URL 返回了一个错误。

http://img2.mukewang.com/6290724200010d3f04170125.jpg

在Controllers/ProfessorsController.php我有那个代码:


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";

}


}

有人可以帮我写代码吗?


POPMUISE
浏览 160回答 2
2回答

繁花如伊

几天前我找到了问题的答案。我把自己复杂化了,就像这样:首先,您在 Templates 文件夹中创建视图。$params 引用 URL。例如:校友是捆绑AlumneController.php然后在 AlumneController.php 中输入不带 .html 的视图名称,然后使用$this->twig = "name_Of_The_View.html";<?php&nbsp;class AlumneController extends Controller{&nbsp; &nbsp; public function process($params)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /*var_dump($params);&nbsp; &nbsp; &nbsp; &nbsp; die();*/&nbsp; &nbsp; &nbsp; &nbsp; if(empty($params[0])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->getAlumne();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }else if(isset($params[0]) && $params[0] == "instruccions"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "instruccions.html";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }else if(isset($params[0]) && $params[0] == "resultats"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "resultats.html";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function getAlumne(){&nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "alumne.html";&nbsp; &nbsp; }&nbsp;}校友.html在按钮或链接中,您用于访问您在其中写入路径的页面<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>AlumneController.php然后在 AlumneController.php 中输入不带 .html 的视图名称,然后使用$this->twig = "name_Of_The_View.html";<?php&nbsp;class AlumneController extends Controller{&nbsp; &nbsp; public function process($params)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /*var_dump($params);&nbsp; &nbsp; &nbsp; &nbsp; die();*/&nbsp; &nbsp; &nbsp; &nbsp; if(empty($params[0])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->getAlumne();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }else if(isset($params[0]) && $params[0] == "instruccions"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "instruccions.html";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }else if(isset($params[0]) && $params[0] == "resultats"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "resultats.html";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*echo $usuari = $_SESSION["username"];*/&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function getAlumne(){&nbsp; &nbsp; &nbsp; &nbsp; $this->twig = "alumne.html";&nbsp; &nbsp; }&nbsp;}校友.html在按钮或链接中,您用于访问您在其中写入路径的页面<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>

守着一只汪

我认为您正在寻找的是Twig 的 API。更具体地说,您需要下面的行来渲染模板,并在数组中传递一些参数:echo&nbsp;$template->render(['the'&nbsp;=>&nbsp;'variables',&nbsp;'go'&nbsp;=>&nbsp;'here']);如果您使用 PHP 将某些内容“打印”到浏览器的最快方式是echo,请不要犹豫使用它。
打开App,查看更多内容
随时随地看视频慕课网APP