How to pass multiple parameters to the `include` tag and what is the correct syntax and separator?

Calendar 👁️ 69

As an experienced website operations expert, I know how to flexibly use the template function in an efficient content management system like AnQiCMS, especiallyincludeLabels are crucial for building maintainable and scalable websites. Today, let's delve deeper into, when you need to go throughincludeHow to pass multiple parameters to a template fragment using the correct syntax and delimiter?

In AnQi CMS template design, we often extract the common parts of the page, such as the header, footer, sidebar, or some reusable content modules, into independent template files.The benefits of doing this are obvious: it improves code reusability, makes the template structure clearer, and greatly reduces the complexity of later maintenance.The core tool that enables this isinclude.

includeThe foundation of tags: the cornerstone of modular templates

First, let's reviewincludeThe basic usage of tags. It allows you to embed the content of one template file into another. For example, if you have a file namedpartial/header.htmlThe page header template, you can introduce it to the main page like this:

{% include "partial/header.html" %}

Sometimes, you may be unsure if the template file to be included exists. To avoid page errors due to the file not existing, you can addif_existskeyword, so if the template file does not exist indeed, it will be gracefully ignored:

{% include "partial/header.html" if_exists %}

This basic introduction method is very convenient for static modules that do not require dynamic changes.But the content of the website is often dynamic, and the template fragments we introduce may need to display different content based on the data passed by the parent template. At this time,includeThe parameter passing ability of the tag is particularly crucial.

Unlock flexibility:withClause passing parameters

In order for the template fragment to receive and process data from the parent template, AnQi CMS'sincludeTags providedwithclause. BywithYou can pass variables from the parent template or new values defined directly to the included template.

Assuming yourheader.htmlYou need a dynamic page title. You can pass a parameter like this:

{% include "partial/header.html" with pageTitle="我们的最新产品" %}

Inheader.htmlInside, you can use it directly as you would access a regular variable{{ pageTitle }}to display this value. For example,header.htmlmay contain code like this:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{{ pageTitle }} - 安企CMS</title>
    <!-- 其他头部元素 -->
</head>
<body>
    <!-- 页面内容 -->
</body>
</html>

Key points: passing multiple parameters and separators

Now, we have come to the core issue of the article: What is the correct syntax and delimiter for the tag when we need to pass multiple parameters at one time?includeHow are the correct syntax and delimiter for the tag?

The answer is very concise and clear: you just need towithcontinue to use after the clausekey=valueto list all the parameters that need to be passed, and these parameters are separated byspacesas a natural separator.

Let's look at a more specific example. If yourheader.htmlPage title is required, as well as page keywords (keywords) and page description (description), you can pass them like this:

{% include "partial/header.html" with pageTitle="安企CMS最新动态" pageKeywords="安企CMS,Go语言CMS,企业建站" pageDescription="安企CMS是基于Go语言开发的企业级内容管理系统,为您提供高效、安全的内容管理解决方案的最新动态。" %}

In this example,pageTitle/pageKeywordsandpageDescriptionare three independent parameters, which are separated by aspacesseparated. Inheader.htmltemplate, you can use them separately{{ pageTitle }}/{{ pageKeywords }}and{{ pageDescription }}to retrieve and display this information.

such space-basedkey=valueThe passing method maintains the high intuitiveness and readability of the syntax, even with a large number of parameters, it can be seen at a glance.

Fine control:onlyThe role of keywords.

In some cases, you may want the template snippet to only access parameterswithpassed explicitly, and not inherit any other variables from the parent template. At this point,onlyKeywords come into play. InwithAdd at the end of theonly, it can create an isolated scope.

For example:

{% include "partial/footer.html" with companyName="安企CMS科技有限公司" only %}

In this case,footer.htmlCan only accesscompanyNameThis variable, while other variables defined in the parent template, such assiteUrlorcurrentUserwill not be able tofooter.htmlUse directly. This is very useful for building highly modular and loosely coupled components, which can effectively avoid variable conflicts or accidental data leaks, enhancing the robustness of the template.

The practice value: Why these details are crucial

For website operation, it is important to understand and use it skillfullyincludeThe mechanism of label parameter passing is not just about mastering a technical detail, but also the key to improving work efficiency and website quality. It enables us to:

  • Build more flexible templates:Customize the content of the imported module easily, without creating a large number of duplicate template files.
  • Improve development efficiency:Team members can develop different template fragments in parallel and then seamlessly integrate them through parameter passing.
  • Optimize content update:When the general module needs to change, only one template file needs to be modified, and it can be adjusted by parameters to meet the needs of different pages, greatly simplifying the content update process.
  • Promote SEO optimization:For different pages, by passing customized TDK (title, keywords, description) through parameters, it helps to improve the search engine's understanding and ranking of the content.

In short, the Anqi CMS'sincludeTag throughwithClauses and clear spacing, providing powerful and intuitive support for the passing of template parameters. Along withonlyThe keyword allows you to finely control the scope of template fragment variables, thereby building a more robust, efficient, and maintainable website.


Frequently Asked Questions (FAQ)

  1. Question: If I amincludeAttempt to access a variable that has not been passedwithWhat will happen if you pass, and there is no variable defined in the parent template?Answer: In the Anqi CMS Django template engine, if you try to access a variable that has not been passed throughwithExplicitly passed clause, which is not in the current parent template scope, the variable is usually parsed asnil(Empty value). It may appear blank when directly outputted in a template, or it may be evaluated as (when used in logical judgments such as){% if variable %}(when evaluated as)falseThis design is robust, usually will not cause program crashes, but will produce the corresponding results according to your template logic.

  2. Question: Besides,includeWhat are some similar template reuse mechanisms of AnQi CMS? What are the differences between them?Answer: AnQi CMS template engine also providesextendsandmacroand other reuse mechanisms.

    • extends:Used for template inheritance, you define a basic skeleton template (parent template) that includes using{% block %}{% endblock %}defined blocks. Sub-templates can use{% extends '父模板路径' %}Inherit the parent template and overwrite (override) or extend these blocks.extendsMore focused on the consistency of the overall page layout and structure.
    • macro:Like a function in programming languages, it allows you to define reusable code snippets and pass parameters.macroHas its own scope, accessible only through variables passed as parameters. It is more suitable for generating repeated HTML structures, such as a general product card or form input box.
    • include:It focuses on directly inserting the content of one template into another, suitable for inserting independent, possibly dynamic parameter components or local content.
  3. Question: When towardsincludeDo the order of parameters passed to the tag have an effect?Answer: No, the order of parameters inwiththe subclausehas no effect. You should usekey=valueParameters are passed in the form of parameters, the template engine will identify and retrieve the corresponding values according to the key name (key), rather than depending on their position in the statement. So, whether it ispageTitle="标题" pageKeywords="关键词"OrpageKeywords="关键词" pageTitle="标题"The effects are all the same.

Related articles

In Anqi CMS template, how can the `include` tag achieve more efficient component-based development and maintenance?

As an experienced website operations expert, I fully understand that in the current era of rapid iteration and high efficiency, the efficiency and maintainability of template development are of great importance to the success of the project.AnQiCMS (AnQiCMS) provides a solid foundation for us with its high-performance architecture based on the Go language and flexible modular design.Among many tools to improve development efficiency, the `include` tag provided by Anqi CMS template is undoubtedly a key tool for achieving more efficient modular development and maintenance.

2025-11-07

How to pass specific variables to an included sub-template without sharing all variables of the parent template?

As an experienced website operations expert, I know that modularization and reusability of templates are crucial in building and maintaining websites.AnQiCMS, with its powerful Django template engine syntax, provides us with great flexibility, allowing us to easily divide the page into manageable small pieces, also known as sub-templates.

2025-11-07

How to ensure that the template file is included when using the `include` tag to avoid page errors?

As an experienced website operations expert, I fully understand how important modularization and reusability are in the construction and maintenance of websites.AnQiCMS (AnQiCMS) leverages its efficient architecture based on the Go language and Django-like template engine, providing great flexibility for content display.Among them, the `include` tag is one of the core functions for implementing template reuse.However, powerful tools also need to be used with caution, otherwise they may bring unexpected 'surprises' to the website - such as page errors caused by the non-existent template files. Today

2025-11-07

In AnQi CMS template, how to modularly reuse the common header and footer code?

In the daily operation of website management, efficiency and maintainability are one of the key factors that determine the success or failure of the project.How to efficiently manage common website elements such as header navigation, footer copyright information, etc., in content management systems (CMS), is a question that every website operations expert will ponder.AnQiCMS (AnQiCMS) provides us with an elegant solution with its flexible template engine and modular design concept.This article will delve into how to use modular strategies in the AnQiCMS template

2025-11-07

How to quickly build a shared sidebar, navigation, or other UI components using the `include` tag?

As an experienced website operation expert, I deeply understand the importance of website efficiency and user experience, and both often cannot be separated from a set of excellent and easy-to-maintain template systems.AnQiCMS (AnQiCMS) with its concise and efficient features, provides us with powerful content management capabilities.Today, let's delve into a seemingly simple yet extremely practical feature—the `include` tag, and see how it helps us quickly build reusable, maintainable website UI components, thereby enhancing overall operational efficiency.--- ## Advanced AnQi CMS

2025-11-07

How does the `include` tag handle the optionality when the template to be included may not exist?

As an experienced website operations expert, I know that the use of templates in a highly efficient and flexible content management system like AnQiCMS is the core of website design and content presentation.It not only determines the appearance of the website, but also directly affects the presentation effect and user experience.Today, let's delve deeply into a practical and elegant little trick in template development: how does AnQiCMS's `include` tag handle this optionality when the template to be included may not exist.

2025-11-07

How can the `include` tag help with the unified management and updating of public content modules in multi-site management?

In the thriving multi-site ecosystem of AnQi CMS, efficiency and consistency are always the focus of operators.For businesses managing multiple brand sites, sub-sites, or content branches, how to ensure content uniqueness and brand consistency while avoiding redundant work, ensuring unified management of public modules, and quick updates is undoubtedly a great challenge.Fortunately, AnQi CMS provides a powerful and flexible solution, which is the indispensable `include` tag in the template engine.

2025-11-07

In the AnQi CMS template, what are the advantages of the `include` tag compared to the traditional copy and paste method?

## Say goodbye to the繁琐repetition: The art and efficiency of the `include` tag in Anqi CMS templates Efficiency and maintainability are key factors in determining the success or failure of modern website operations.As an experienced website operation expert, I am well aware of how an excellent content management system (CMS) silently supports the brilliant presentation of the frontend in the background.

2025-11-07