What role does the `extends` tag play in the AnQiCMS template inheritance system?

Calendar 👁️ 65

In AnQiCMS template development,extendsLabels play a core role, being the key to building efficient, maintainable, and structurally unified web templates. It can beextendsThe label is understood to create a bridge between "master" and "child pages", allowing you to easily define a common layout skeleton for the entire website without repeating a large amount of the same code on each page.

Imagine, a website usually has a fixed header, footer, and sidebar that are consistent on most pages.If you do not use template inheritance, you may need to copy and paste these common codes in each HTML file, which is not only inefficient but also prone to errors once you need to make changes (such as updating the navigation menu or copyright information), and you have to change them one by one in all files.

AndextendsThe label is exactly to solve this problem.The function is to let a child template (Child Template) inherit all content and structure from a parent template (Parent Template).When a child template declares inheritance from a parent template, it automatically gains everything defined in the parent template, including HTML structure, CSS links, JavaScript references, and so on.The sub-template does not need to repeat these common parts, just focus on the unique content of itself.

The key to implementing this inheritance mechanism lies in the parent template.blockTag. In the parent template, you can use{% block 块名称 %}{% endblock %}Define some content placeholders that can be filled or overridden by child templates. For example, you can define atitleblock for page titles, acontentThe block is used for the main content of the page. The parent template provides a default content for these blocks (even if it is blank) to ensure the integrity of the page.

When a child template inherits from a parent template, it can override or extend the corresponding area in the parent template by defining the sameblocktags. For example, the child template can be written as:

{% extends 'base.html' %} {# 继承名为'base.html'的父模板 #}

{% block title %}
    <title>我的具体页面标题</title> {# 覆盖父模板中的title块 #}
{% endblock %}

{% block content %}
    {# 这里是这个页面特有的主体内容 #}
    <p>欢迎来到我的网站!</p>
{% endblock %}

This, the sub-templateindex.htmlwas usedbase.htmlThe page skeleton defined, and only these two areas were modifiedtitleandcontentThese two areas, the rest were not explicitly covered by the sub-templateblockThe region (such as the header, footer) will follow the default content of the parent template.

extendsThe advantages of tags are self-evident:

  1. Improve development efficiency:Developers only need to focus on the unique content of the page, reducing a lot of repetitive HTML code writing work, thereby accelerating the development process of the website.
  2. Simplify maintenance work:When the public part of the website (such as the navigation bar structure, footer copyright information) needs to be updated, it is only necessary to modify the parent template once, and all child templates inheriting it will be automatically updated, greatly reducing maintenance costs and error rates.
  3. Ensure website consistency:Enforce a unified layout and structure across all pages, which helps maintain the overall style and brand image of the website, enhancing user experience.
  4. Optimize code structure:Separate common code from page-specific content, making template files clearer, modular, easier to understand and manage.

While usingextendsWhen labeling, there are several important **practices. First,extendsThe tag must be the first tag in the sub-template, any HTML code or template tag before it will cause inheritance to fail. Secondly, try to abstract elements that appear repeatedly on multiple pages into the parent template and useblockLabel the area reserved for all possible content changes in the sub-template. This ensures that the parent template is sufficiently general, and the sub-template is sufficiently concise.

In summary,extendsTags are the foundation of the AnQiCMS template system, which realizes the concept of 'write once, reuse anywhere'.It elegantly separates layout from content, greatly enhancing the development efficiency, maintainability, and consistency of templates, and is an indispensable tool for building professional-level websites.


Frequently Asked Questions (FAQ)

  1. WhyextendsMust the label be the first label in the template? extendsThe tag's role is to define the inheritance relationship of the current template, it indicates that the template engine should first load and parse the parent template, and then apply the content of the child template on this basis. IfextendsThe tag is not the first, the template engine may have started processing or outputting other content before it is parsed, which may lead to the incorrect establishment of inheritance logic, causing parsing errors or page structures that do not meet expectations.

  2. I can add what the parent template does not define in the sub-templateblockCan I add the HTML content of the tag?It is not recommended to do this. In the child template inheriting from the parent template, all HTML content belonging to the child template itself should be placed inside someblocktag. If the content is placed inside anyblockOutside the tag, this content is usually ignored or incorrectly rendered because the template engine considers it to be 'orphaned' content that does not belong to any covered area.

  3. If I define the default content in the parent template'sblockbut the child template has not rewritten thisblockhow will the page display?If the child template inherits the parent template but does not explicitly rewrite a certainblockLabel, then thatblockThe area will continue to use the default content defined in the parent template.This is the embodiment of template inheritance flexibility, which allows you to provide fallback content for all possible areas in the parent template, ensuring that even if the child template does not make any customization, the page can still display a basic version.

Related articles

How does the `macro` function in AnQiCMS templates help me reduce redundant rendering logic?

In Anqi CMS template development, improving efficiency and maintaining code cleanliness is the goal that every developer is pursuing.web pages often contain many structurally similar but content-wise different areas, such as each article card in the article list, the various features of a product detail page, or each link item in the navigation menu.If each time you have to rewrite this similar rendering logic, it not only takes time and effort, but also, once you need to make modifications, you may face the dilemma of having to make repetitive changes in multiple places.Luckyly, AnQiCMS's powerful template system provides `macro` macro function

2025-11-08

How to pass specific variables or data when including a sub-template?

Advanced AnQiCMS Template: `include` Sub-template Data Passing Techniques and Practices In AnQiCMS template development, the `include` tag is undoubtedly a powerful tool for enhancing template reusability and modularization.It allows us to extract common code snippets (such as page headers, footers, sidebars, etc.) and then introduce them where needed, thus avoiding repetition and making the template structure clearer and maintenance more efficient.However, the sub-templates introduced often need to display different content, which raises a core question: how to introduce sub-templates at the time

2025-11-08

How to use the `include` tag to reuse header, footer, and other common code segments in AnQiCMS templates?

Building website templates in AnQiCMS, efficient code reuse is the key to improving development speed and maintenance efficiency.Imagine, the header (Header) and footer (Footer) of a website appear almost on every page. If the same code is written on each page, it is not only time-consuming, but also, once a modification is needed, it has to be searched and updated on each page, which is undoubtedly a huge amount of work.

2025-11-08

How to safely output content containing HTML tags in AnQiCMS templates (e.g., article content)?

Guide to safely output HTML content in AnQiCMS templates When building a website with AnQiCMS, we often need to display content with rich formatting, such as article content, product descriptions, category details, or single-page content.This content is usually entered through the backend rich text editor, which will naturally contain HTML tags such as `<p>`, `<strong>`, `<em>`, `<a>`, and so on.How to correctly and safely output this content with HTML tags in the front-end template is a very important issue.If handled improperly

2025-11-08

How to output the current date and time in the AnQiCMS template and specify the format?

Displaying the date and time in the AnQiCMS template in a specific format is a common requirement in website content operations.Whether it is to display the publication time of articles, the deadline of events, or show the current year in the footer, accurate and beautiful time information can enhance the user experience.AnQiCMS provides simple and efficient template tags, allowing you to easily implement these features. Next, we will discuss in detail how to output the current date and time in the AnQiCMS template and specify the format you need.

2025-11-08

How to output variables in a template and safely escape content to prevent XSS attacks?

In the process of managing and displaying website content, ensuring the security of user data is a crucial link.It is an issue that every website operator needs to pay attention to when the website needs to display user submitted content or data obtained from external sources, how to effectively prevent cross-site scripting (XSS) attacks.AnQiCMS (AnQiCMS) took this into full consideration from the very beginning, providing solid security guarantees for content output through its powerful template engine and flexible filter mechanism.### Cross-Site Scripting Attack

2025-11-08

How to display the latest N articles or products on the homepage and implement pagination control?

## Asecurity CMS: The Practice of Efficiently Displaying the Latest Content on the Homepage and Implementing Pagination The homepage is an important entry point for visitors to understand the site content and obtain the latest information, it is crucial to clearly and effectively display the latest published articles or products.The AnQi CMS provides powerful and flexible template tags, which help us easily achieve this goal, and can also fine-tune content pagination to ensure a smooth user experience.

2025-11-08

How to filter and loop through the document list under a specific category based on the category ID?

It is crucial to organize and present content when building and operating a website.Especially when the content of a website becomes increasingly rich, how to enable visitors to quickly find the information they are interested in and clearly browse all articles under a specific theme has become a carefully designed problem.AnQiCMS (AnQiCMS) provides powerful and flexible content management capabilities, allowing us to easily achieve precise control and display of document lists.

2025-11-08