Twig Template enginetemplate engine twig templates are normal text files, do not need special extension,. html. htm. Twig can be. Variables and expressions within the template are parsed and replaced at run time, and tags will control the logic of the template. Installing twig
Command line run:
" twig/twig:~1.0 "
Create twig.php under the Services directory:
<?PHP/** * Class Twig*/classtwig{ Public $view; Public $data; Public $twig; Public $path= Base_path. '/app/views/'; /** * Twig constructor. * @param $view * @param $data*/ Public function__construct ($view,$data) { $loader=NewTwig_loader_filesystem ($this-path); $this->twig =NewTwig_environment ($loader,Array( ' Cache ' = Base_path. '/cache/views/', ' debug ' =true )); $this->view =$view; $this->data =$data; } /** * @param $view * @param array $data * @return Twig*/ Public Static functionRender$view,$data=Array()) { return NewTwig ($view,$data); } Public function__destruct () {$this->twig->display ($this->view,$this-data); }}
Execute command:
Composer Dump-autoload
Create a App.twig file and a Index.twig file (. html file) under app/views/, respectively:
{#app. twig#}<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>App</title></Head><Body><Header>Header</Header>{% block content%}{% endblock%}<Footer>Footer</Footer></Body></HTML>
{#index. twig#} {% extends ' App.twig '%} {% block content%}hello {{Data.name}}, your mobile is {{data.mobile}}{% endblock%}
In the controller, add:
$data = [' data ' = = [' Name ' = ' Evai ', ' mobile ' = 12345678910]]; return $data);
To refresh the browser, you can see the following page displayed:
Please refer to the Twig website for detailed documentation.
Build Your own PHP framework (twig template engine)