Build Your own PHP framework (twig template engine)

Source: Internet
Author: User
Tags php framework

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.