In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use templates in Symfony2", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use templates in Symfony2" this article.
The details are as follows:
As we know, controller is responsible for handling every request to enter a Symfony2 application. In fact, controller delegates most of the heavy work elsewhere so that the code can be tested and reused. When a controller needs to generate HTML,CSS or other content, it gives that work to a templating engine.
Template:
A template is just a text file that can generate any text format (HTML,XML,CSV,LaTex...). The most famous type of template is the PHP template, a text file that can be parsed by PHP, which mixes text and PHP code.
Welcome to Symfony! Home Blog
This template defines that the basic HTML initial document is a simple two-column page. There are three {% block%} definitions on this page, defining title,sidebar and body, respectively. Each block can be overridden by inheriting its subtemplates or retain its current default implementation. The template can also be rendered directly, but only shows the definition of the underlying template.
Define a subtemplate below:
Twig format:
{# src/Acme/BlogBundle/Resources/views/Blog/index.html.twig #} {% extends':: base.html.twig'%} {% block title%} My cool blog posts {% endblock%} {% block body%} {% for entry in blog_entries%} {{entry.title}}
{{entry.body}}
{endfor} {endblock}
PHP code format:
The parent template is represented by a special string syntax:: base.html.twig, which indicates that the template is in the project's app/Resources/views directory.
Keyword {% extends%} tag inherited by the template. This tag tells the templating engine to evaluate the parent template first, which sets the layout and defines multiple blocks. Then there is the child template. In the above example, the two blocks of title and body defined in the parent template will be replaced by the definition in the child template. Depending on the value of blog_entries, the output is as follows:
My cool blog posts Home Blog My first post
The body of the first post.
Another post
The body of the second post.
We notice here that because the block of sidebar is not defined in the child template, the content from the parent template is displayed and is not replaced by the child template. The {% block%} tag located in the parent template is the default value, and it will be used as the default value if there is no override by the child template.
You can carry out multi-tier inheritance according to your needs. A three-tier inheritance model is generally used in Symfony2 projects to organize templates and pages. When we use template inheritance, we need to be aware of:
If you use {% extends%} in a template, it must be the first label of the template.
The more {% block%} in your base template, the better. Remember, child templates don't have to wait for all the block in a parent template. The more block is defined in your parent template, the more flexible your layout will be.
If you find duplicate content in multiple templates, this may mean that you need to define a {% block%} in the parent template for that content. Sometimes a better solution might be to put this content in a new template and then include it in that template.
If you need to get the contents of a block from the parent template, you can use the {{parent ()}} function.
{% block sidebar%} Table of Contents... {{parent ()}} {% endblock%}
Naming and storage location of the template
By default, templates can be saved to two locations:
Under the app/Resources/views directory, you can store the entire application-level base template as well as those templates that rewrite the bundle template.
Under path/to/bundle/Resources/views directory, each bundle has its own template.
Symfony2 uses bundle:controller:template string syntax to represent templates. This can represent many different types of templates, each stored in a specific path:
AcmeBlogBundle:Blog:index.html.twig is used to specify a template for a specific page.
AcmeBlogBundle stands for bundle, indicating that the template is located in AcmeBlogBundle, such as src/Acme/BlogBundle.
Blog stands for BlogController, and specifies that the template is located in the blog subdirectory of Resourcs/views, with index.html.twig as the template name.
Assuming that AcmeBlogBundle is located in src/Acme/BlogBundle, the final path will be: src/Acme/BlogBundle/Resources/views/Blog/index.html.twig
AcmeBlogBundle::layout.html.twig this notation points to the base template of AcmeBlogBundle. There is no controller section, so the template should be located in the Resources/views/layout.html.twig of AcmeBlogBundle
:: base.html.twig represents an application-level base template or layout file. Note that the statement begins with a colon, which means that there are no bundle and controller parts, which means that the file is not in a bundle, but in the root directory app/Resources/views
In the section rewriting Bundle templates, you will find how templates located in AcmeBlogBundle are rewritten with the same name by all templates located in the app/Resources/AcmeBlogBundle/views/ directory
This approach gives us a powerful way to rewrite vendor-supplied bundle templates.
Template suffix (suffix)
The bundle:controller:template syntax indicates where each template file is stored. Each template name also has two extensions to specify the format and template engine.
AcmeBlogBundle:Blog:index.html.twig HTML format, Twig engine
AcmeBlogBundle:Blog:index.html.php HTML format, PHP engine
AcmeBlogBundle:Blog:index.css.twig CSS format, Twig engine
By default, any template for Symfony2 can be written as Twig or PHP engine, which is determined by the suffix. The first part of the suffix (.html, .css) represents the resulting format.
Tags and helper classes
You have a basic understanding of templates, how they are named, how to use template inheritance, and so on. The hardest part to understand is over. Next we will learn about a large number of tools available to help us perform the most common template tasks such as including another template, linking to a page, or including an image.
Symfony2 contains blood multi-serialized Twig tags and functions to help designers get the job done more easily. In PHP, the template system provides an extensible helper system that provides a lot of useful content in the context of templates. We have seen some built-in Twig tags, such as {% block%} {% extends%}, and the PHP helper $view ['slots'].
Include additional templates:
You may often want to include the same template or code snippet in multiple different pages. For example, if there are "news articles" in an application, the template code may use a detailed page for an article, a page for the most popular articles in reality, or a list page for the latest articles.
When you need to reuse some PHP code, you usually put it in a PHP class or function. You can do the same in the template. By putting reusable code in one of its own templates, and then including that template in other templates. For example, we create a reusable template as follows:
Twig format:
{# src/Acme/ArticleBundle/Resources/views/Article/articleDetails.html.twig #} {{article.title}} by {{article.authorName}}
{{article.body}}
PHP code format:
By
Then we include it in other template definitions:
Twig format:
{# src/Acme/ArticleBundle/Resources/Article/list.html.twig #} {% extends' AcmeArticleBundle::layout.html.twig'%} {% block body%} Recent Articles {% for article in articles%} {% include 'AcmeArticleBundle:Article:articleDetails.html.twig' with {' article': article}%} {% endfor%} {% endblock%}
PHP code format:
Recent Articles
The template contains using the {% include%} tag. The name of the template should be in a common way. The article variable is used in the articleDetails.html.twig template, which is passed in here by using the with command in the list.html.twig template. The {'article':article} syntax is written for a standard Twig hash map. If we need to pass multiple elements, we can write {'foo': foo,' bar': bar}.
Embed Controllers
In some cases, you need to do more than include a simple template. Suppose you have a menu bar sidebar to display the latest articles in your layout file. Getting three of the latest articles may require querying the database or performing other operations that contain a lot of logic so that they cannot be done in a template. The solution to this situation is to simply type a complete controller into your template. First create a controller to render a specific number of recent articles:
/ / src/Acme/ArticleBundle/Controller/ArticleController.phpclass ArticleController extends Controller {public function recentArticlesAction ($max = 3) {/ / generate a database call or other logic to get the code of $max for the latest article $articles =..; return $this- > render ('AcmeArticleBundle:Article:recentList.html.twig',array (' articles'= > articles));}}
The recentList template is quite simple:
Twig format:
{# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #} {% for article in articles%} {{article.title}} {% endfor%}
PHP code format:
Whenever you need a variable or some column of information, you don't have to access it in the template, but consider rendering a controller. Because Controller can execute faster and improve the organization and reuse of code.
Link to the page:
It is common for a template to create a link to another page in your application. We use the path Twig function to generate URL based on the routing configuration rather than hard-coding URL in the template. If you want to modify the URL of a particular page in the future, you only need to change the routing configuration, and the template will automatically generate a new URL. For example, if we intend to link to the "_ welcome" page, first define its routing configuration:
YAML format:
_ welcome: pattern: / defaults: {_ controller: AcmeDemoBundle:Welcome:index}
XML format:
AcmeDemoBundle:Welcome:index
PHP code format:
$collection = new RouteCollection (); $collection- > add ('_ welcome', new Route ('/', array ('_ controller' = > 'AcmeDemoBundle:Welcome:index',); return $collection
We can use the Twig function path in the template to reference this route to link to the page.
Twig format:
Home
PHP code format:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.