How to use the `include` tag in AnQiCMS templates to implement modular display of common components (such as headers and footers)?

Calendar 👁️ 76

When building a website, we often encounter such situations: the header (Header), footer (Footer), and some sidebars, navigation menus, and other components almost appear on every page.If each of these common sections of code is written repeatedly, it not only takes time and effort, but also, once a modification is needed, it must be searched and updated page by page, which is inefficient and prone to errors.includeThe tag is the core tool for realizing the modular display of public components.

includeThe role of a tag, as the name implies, is to embed an independent template file (which can be understood as a code snippet or component) into another template file.In this way, we can save the repetitive structures on the website, such as navigation bars, copyright information, side advertisement slots, and so on, as separate template files.includeTag references without copying code, greatly improves the maintainability and development efficiency of templates.

How specifically should it be usedincludeWhat about the tags

First, organize the template file properly.The AnQi CMS recommends placing public code and code snippets in a specific directory.partialfolder, place all reusable components such asheader.html/footer.html/sidebar.htmletc., together in it. The benefit of doing this is that the structure is clear and easy to manage.

The basic syntax for introducing public components is very concise:{% include "partial/header.html" %}This line of code will tell Anqi CMS to insert herepartial/header.htmlAll content defined in the file. When the browser accesses the page containing this line of code, it will render the page header content.

While usingincludeWhen labeling, we also need to consider some special cases. For example, if you are not sure whether a public component file exists, you can directlyincludeadd aif_existsmodifier:{% include "partial/navigation.html" if_exists %}so ifpartial/navigation.htmlThe file does not exist, the system will not report an error, but will silently ignore this part of the reference, to avoid page rendering failure due to missing files.

Further, public components often need to display different content based on the context of the current page.For example, the page header may need to display the title of the current page or different navigation items based on the user's login status.includelabel'swithKeywords come into play. ThroughwithThe keyword, we can pass to beincludepass specific variables to the template:{% include "partial/header.html" with pageTitle="产品详情页" userName="张三" %}Inpartial/header.htmlIn the template file, we can use it directly{{pageTitle}}and{{userName}}to retrieve and display these passed values.

By default, theincludeThe template inherits all variables from the parent template. But sometimes, to avoid variable conflicts or to have stricter control over the range of variables passed, you can useonlykeyword. WhenonlywithwithUsed simultaneously, it isincludeThe template will only acceptwithVariables explicitly specified in it, and will not inherit other variables from the parent template:{% include "partial/header.html" with pageTitle="新闻中心" only %}Thus,header.htmlCan only access inpageTitleand cannot access other variables that may exist in the parent template.

In a practical project,includeThe application scenarios of the tag are very extensive. In addition to the header, footer, and sidebar mentioned above, it can also be used for:

  • Navigation menu:Whether it is a top navigation, bottom navigation, or multi-level dropdown menu, it can be made into an independent component.
  • Copyright information:Unified management of the website's copyright statement, filing number, etc.
  • Universal form elements:Such as search boxes, subscription email forms, etc.
  • Breadcrumbs navigation:As an independent module for dynamically generating paths.

By using this modular development approach, the template code of the website will become cleaner and more structured, greatly reducing maintenance costs.When the website needs to be redesigned, only a few public component files need to be modified to achieve a full-site update, greatly improving work efficiency.At the same time, it also promotes collaboration among team members, where each person can focus on the development of their respective modules and then integrate them together.

Of course, when usingincludeWhen labeling, there are also some small suggestions. First, maintainpartialThe directory structure is organized, avoiding chaotic component files.Secondly, name variables reasonably and pay attention to the type and value of variables when passing them.In the end, while modularization is convenient, it is also necessary to avoid over-dividing. For code snippets that appear only once or are logically very simple, writing them directly on the page may be more efficient.


Frequently Asked Questions (FAQ)

1.includeTags andextendsWhat are the differences between tags? includeThe tag is used to insert a template file completely into a specific position in another template, it is more focused on code snippet reuse.You can imagine putting an independent small box (component) into a large box (main page).extendsTags are used for template inheritance, defining a basic skeleton template (parent template), which other templates (child templates) can inherit and rewrite specific 'blocks' (blocks) defined in the parent template.extendsFocuses more on the overall reuse and customization of the page structure. Typically, a page template willextendsstart with a basic layout, thenincludesome common components.

2. How toincludeHow to get the current page data from the incoming public component?There are two main methods. The first is the default mechanism: beingincludeThe template inherits all context variables from the parent template, so in most cases, no additional operation is required, and the common components can access the data of the current page. For example, if the parent page defines{{siteName}}, wasincludeThe header template can be used directly. The second is to usewithKeyword explicitly pass: When you want to pass specific data, or want to control the scope of variables, you can inincludeUsed in tagswithKeywords, for example:{% include "partial/header.html" with currentPage="首页" %}.header.htmlIn,{{currentPage}}you can directly use

3. If I am in the header component (partial/header.htmlWill variables defined in this context affect other page content?Generally, they will not.includeVariables defined within a template that is introduced, generally have a scope limited to the template itself. Once the template is rendered, the scope of these internal variables also ends and will not leak to the parent template or other templates that areincludein the template. But if you are modifying variables inherited from the parent template (for exampleinclude), this change may only affect the{% set siteName = "新的站点名" %}.includeThe component is valid internally, the specific behavior depends on the implementation details of the template engine, and it is a safe practice to avoid modifying inherited variables arbitrarily within the component.

Related articles

How to define the basic layout in AnQiCMS templates and use the `extends` tag to unify the display style?

When building a website, maintaining a unified page style and clear structure is crucial for improving user experience and development efficiency.AnQiCMS provides a powerful and flexible template engine, which draws on the excellent ideas of Django templates, especially the use of the `extends` tag, making this goal achievable. ### The Foundation of a Unified Style: Basic Layout Template Imagine that your website is like a meticulously designed building, where each room has unique decorations, but the load-bearing walls, roof, and foundation are unified.

2025-11-07

How does AnQiCMS control the timing of content display on the website?

In content operation, the timing of content release is often a key factor in determining its effectiveness.In order to coordinate with marketing activities, meet traffic peaks, or simply to maintain the continuous update of website content, manual timing release not only takes time, but is also prone to errors.AnQiCMS knows this very well, therefore it provides a powerful and easy-to-use scheduled publishing feature, aimed at helping you easily solve the problem of content going live, ensuring that your content is always presented to your target audience at the right moment.When you create new articles, product introductions, or any other documents in the AnQiCMS backend

2025-11-07

How can AnQiCMS use SEO tools to improve the visibility of content in search engine results?

In the vast realm of digital marketing today, content creation is indeed important, but how to make these carefully crafted contents discovered by the target audience is the key to realizing their value.Search engine optimization (SEO) is the bridge connecting content to readers, and AnQiCMS, as an enterprise-level content management system, is well-versed in this, built-in with many powerful tools, aimed at helping users improve the visibility of their content in search engine results. We will delve deeper into how AnQiCMS can gain more exposure for your website content through its unique features.###

2025-11-07

What specific optimization effect does the static rules of the website have on the display of AnQiCMS page URLs?

In website operation, the page URL (Uniform Resource Locator) often plays a more important role than we imagine.It is not only the entrance for users to access the website, but also the key clue for search engines to understand and collect website content.For a content management system like AnQiCMS, how to effectively optimize the URL structure is directly related to the visibility and user experience of the website.The application of pseudo-static rules is a core optimization strategy.What is the rule of pseudo-static?Why is it so important in AnQiCMS?

2025-11-07

How to use the `for` loop tag in AnQiCMS template to iterate and display content list?

In AnQiCMS, efficiently displaying content lists is a core requirement for website operation.Whether it is articles, products, categories, or custom data, we need a flexible mechanism to traverse and present this information.AnQiCMS's powerful template engine provides a `for` loop tag, which acts as a helpful assistant, making it easy for us to achieve this goal.

2025-11-07

How to filter and display the document list of a specified category or model with the `archiveList` tag in AnQiCMS?

AnQiCMS as an efficient enterprise-level content management system, provides users with flexible and diverse content management and display capabilities.When building a website, we often need to display document lists based on specific conditions, such as only showing articles in a particular category, or only listing entries of specific content models (such as products, services).At this time, the `archiveList` tag has become a powerful tool in our hands.It is not only powerful but also very intuitive to use, making it easy for us to meet these needs.### `archiveList`

2025-11-07

How to add pagination and optimize the display effect for document lists in AnQiCMS?

In website operation, how to efficiently display a large amount of content while ensuring the smoothness of user experience is a topic that cannot be ignored.The pagination feature of the document list is the key to solving this problem.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides intuitive and powerful template tags, allowing you to easily add pagination features to document lists and optimize the display on top of that, making your website content more attractive.

2025-11-07

How to retrieve and display the complete content and properties of a single document using the `archiveDetail` tag?

When using AnQiCMS to build website content, displaying the complete information of a single document is one of the core requirements.No matter whether it is the detailed content of an article, a detailed introduction of a product, or any other specific entry of a custom content model, a highly efficient and flexible way is needed to extract and present this data.This is where the `archiveDetail` tag comes into play.

2025-11-07