Home > Article > Backend Development > How to use template engine Twig with Silex framework?
In the process of Web development, using a template engine can greatly reduce the workload of front-end development and also enhance the maintainability of Web applications. Twig is a popular PHP template engine that is simple, easy to read, and highly scalable. This article will introduce how to use the Twig template engine in the Silex framework.
First, we need to install Twig using Composer. Enter the project directory and execute the following command:
composer require twig/twig
After the installation is complete, we need to register the Twig service provider in Silex:
// index.php require_once __DIR__.'/vendor/autoload.php'; $app = new SilexApplication(); // 注册Twig服务提供器 $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', ));
In the above code, we use register
Method to register the Twig service provider into the Silex application and specify the directory where the Twig template files are stored.
There are two very important concepts in Twig: templates and variables. Templates are files that describe how to render data, and variables are the data we want to use in the template.
Let’s create a simple template file:
<!-- views/hello.html.twig --> <!DOCTYPE html> <html> <head> <title>Hello Twig</title> </head> <body> <h1>Hello {{ name }}!</h1> </body> </html>
We can use the {{}}
syntax provided by Twig to insert variables, as in the above code The {{ name }}
means rendering the value of the name
variable to the position in the template.
Next, we create a route in Silex that will render the above template file. The specific code is as follows:
// index.php require_once __DIR__.'/vendor/autoload.php'; $app = new SilexApplication(); $app->register(new SilexProviderTwigServiceProvider(), array( 'twig.path' => __DIR__.'/views', )); // 定义路由 $app->get('/hello/{name}', function ($name) use ($app) { return $app['twig']->render('hello.html.twig', array( 'name' => $name, )); }); $app->run();
In the above code, we use $app['twig']
to obtain the Twig instance, and use the render
method to render the template file, and at the same time Pass the value of the name
variable in the template.
Visit http://localhost:8000/hello/world and you will get output similar to the following:
<!DOCTYPE html> <html> <head> <title>Hello Twig</title> </head> <body> <h1>Hello world!</h1> </body> </html>
Twig also supports combining multiple templates in Rendered together, this makes it very convenient to separate the structure and content of the template. For example, we can separate the head and bottom of the website and save them as header.html.twig
and footer.html.twig
respectively, and then nest them in other templates .
Let's create a template file for displaying blog posts:
<!-- views/post.html.twig --> {% extends 'layout.html.twig' %} {% block content %} <h1>{{ title }}</h1> <div>{{ content }}</div> <p>Author: {{ author }}</p> {% endblock %}
In this template, we use {% extends 'layout.html.twig' %} The
directive to inherit another template means that the content in the template will be inserted into the block named content
in layout.html.twig
.
Next we write the layout.html.twig
template to define the layout of the blog post:
<!-- views/layout.html.twig --> <!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <header> <h1>My Blog</h1> </header> <main> {% block content %}{% endblock %} </main> <footer> © 2021 Me </footer> </body> </html>
In the above code, we define a file named ## The #title block uses the
{% block %} command provided by Twig.
{% block content %}{% endblock %}, because when submitting POST, the content here will be replaced with
post. in html.twig
4a249f0d628e2318394fd9b75b4636b1{{ title }}473f0a7621bec819994bb5020d29372adc6dce4a544fdca2df29d5ac0ea9906b{{ content }}16b28748ea4df4d9c2150843fecfba68e388a4556c0f65e1904146cc1a846beeAuthor: {{ author }} 94b3e26ee717c64999d7867364b1b4a3.
post.html.twig Template:
$app->get('/post/{id}', function ($id) use ($app) { $post = array( 'title' => 'Post ' . $id, 'content' => 'This is the content of post ' . $id, 'author' => 'Me', ); return $app['twig']->render('post.html.twig', $post); });In the above code, we create a new route named ## Array of #$post
, which contains the title, content, author and other information of the article. Then, use Twig's render
method to render the post.html.twig
template while passing the $post
array to the template. Eventually, we will see output similar to the following:
<!DOCTYPE html> <html> <head> <title>Post 1</title> </head> <body> <header> <h1>My Blog</h1> </header> <main> <h1>Post 1</h1> <div>This is the content of post 1</div> <p>Author: Me</p> </main> <footer> © 2021 Me </footer> </body> </html>
Summary
The above is the detailed content of How to use template engine Twig with Silex framework?. For more information, please follow other related articles on the PHP Chinese website!