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

Calendar 👁️ 76

As an experienced website operations expert, I fully understand the importance of website efficiency and user experience, and these two are often inseparable from a set of excellent, easy-to-maintain template systems.AnQiCMS (AnQiCMS) with its concise and efficient characteristics, provides us with powerful content management capabilities.Today, let's delve deeply into a seemingly simple yet extremely practical feature -includeLabel, see how it helps us quickly build reusable, easy-to-maintain website UI components, thereby improving overall operational efficiency.


Anqi CMS Advanced: How to Use SkillfullyincludeTag builds efficient and reusable UI components

In the development and operation of modern websites, we often encounter such a scenario: the sidebar, top navigation, footer information, and other elements of the website appear on almost every page, but the content may be slightly different.If every page repeats the writing of this code, it is not only inefficient, but also a nightmare for subsequent maintenance.This is, Anqi CMS'sincludeTags are like a wizard, able to transform these repetitive tasks into magical modular management.

includeTags: The foundation for modular website construction

The Anqi CMS template engine syntax is similar to Django, it allows us to go throughincludeLabel, embed an independent code snippet (usually called a "fragment" or "component") into another template file. Imagine your website as a set of Lego blocks,includeLabels are the interfaces that connect these building blocks, allowing you to easily insert pre-made “sidebar blocks”, “navigation bar blocks”, and others into any page “base” you want to place.

The core benefit of doing this is:Efficiency, consistency, and maintainability.

  • efficiency: Write once, use many times, saving a lot of time copying and pasting.
  • Consistency: All references to the sameincludeThe component of the file maintains consistent style and structure, avoiding a scattered visual sense.
  • maintainabilityWhen you need to modify a shared component, you only need to modify one file, and all the pages that reference it will automatically update, greatly reducing maintenance costs.

Set up your modular component workspace

In AnQi CMS, all template files are stored in/templateUnder the root directory. To better organize and manage these reusable components, we usually create a subdirectory namedpartial. For example, the structure of your template directory may look like this:

/template
└── your_theme_name/
    ├── config.json
    ├── index/
    │   └── index.html
    ├── partial/
    │   ├── header.html
    │   ├── footer.html
    │   └── sidebar.html
    └── ... 其他模板文件

Here, header.html/footer.htmlandsidebar.htmlIt is what we are preparing to pass throughincludeComponents introduced by tags. Remember that all template files should end.htmlwith a suffix and use UTF-8 encoding to ensure normal display of content.

includeBasic Usage and Advanced Techniques of the Tag

Basic Introduction: Get Components Moving

UseincludeThe tag is very intuitive, you just need to specify the path to the template file you want to include:

{# 假设这是你的页面主体模板文件,如 index/index.html #}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>我的安企CMS网站</title>
    {# 引入 CSS 和 JS 等静态资源,这里使用系统标签 TemplateUrl 获取模板静态文件地址 #}
    <link rel="stylesheet" href="{% system with name='TemplateUrl' %}/css/style.css">
</head>
<body>
    {# 引入顶部导航 #}
    {% include "partial/header.html" %}

    <main class="main-content">
        <div class="left-col">
            {# 引入侧边栏 #}
            {% include "partial/sidebar.html" %}
        </div>
        <div class="right-col">
            <h1>欢迎来到我的网站!</h1>
            <p>这里是页面的主要内容区域。</p>
        </div>
    </main>

    {# 引入页脚 #}
    {% include "partial/footer.html" %}
</body>
</html>

In the above example,header.html/sidebar.htmlandfooter.htmlThe content will be embedded into the corresponding position of the main page template.

Pass data: Make the component smarter

Most of the time, our shared components need to display content dynamically according to the context of the current page. At this point,includelabel'swithParameters come into play. It allows you to pass custom variables to the template being imported:

{# 在页面主体模板中,向侧边栏传递一个标题 #}
{% include "partial/sidebar.html" with sidebar_title="最新文章推荐" %}

and in thepartial/sidebar.htmlIn it, you can use it like a regular variable:

{# partial/sidebar.html 文件内容 #}
<aside class="sidebar">
    <h2>{{ sidebar_title }}</h2> {# 这里会显示“最新文章推荐” #}
    <ul>
        {% archiveList archives with type="list" limit="5" order="id desc" %}
            {% for item in archives %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</aside>

You can also pass multiple variables, even throughonlyParameter limitation only passes the specified variables to prevent unnecessary variable pollution from being introduced into the template namespace:

{% include "partial/header.html" with current_page="about" user_name="访客" only %}

Graceful degradation: Avoid page crash

If a certainincludeThe file may not exist, to avoid page errors, you can useif_existsparameter. In this way, if the file is missing, Anqicms will silently ignore it instead of interrupting the page rendering:

{# 如果 special_promo.html 存在则引入,否则忽略 #}
{% include "partial/special_promo.html" if_exists %}

Combine AnQi CMS tags to build rich components

AnQi CMS has built-in rich template tags, which can be combined withincludePerfectly combined to build powerful shared components.

  • Navigation bar (partial/header.html): usually usednavListtags to get the website navigation data.

    `twig {# partial/header.html #}

    <div class="logo">
        <a href="/"><img src="{% system with name='SiteLogo' %}" alt="{% system with name='SiteName' %}" /></a>
    </div>
    <nav>
        <ul>
            {% navList navs with typeId=1 %} {# 假设typeId=1是主导航 #}
                {% for item in navs %}
                    <li class="{% if item.IsCurrent %}active{% endif %}">
                        <a href="{{ item.Link }}">{{ item.Title }}</a>
                        {% if item.NavList %} {# 如果有二级导航 #}
                            <ul class="submenu">
                                {% for sub_item in item.NavList %}
                                    <li><a href="{{ sub_item.Link }}">{{ sub_item.Title }}</a></li>
                                {% endfor %}
                            </ul>
                        {% endif %}
    

Related articles

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

As an experienced website operations expert, I am well aware that how to flexibly use the template function in a high-efficiency content management system like AnQiCMS, especially the `include` tag, is crucial for building a maintainable and easily expandable website.Today, let's delve into the correct syntax and separator when you need to pass multiple parameters to a template fragment using the `include` tag.

2025-11-07

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

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

Does the `include` tag support dynamic filenames, i.e., deciding which template file to include based on the variable value?

## Unveiling the `include` tag of AnQi CMS: Can the dynamic template filename work?As a senior website operations expert, I have a deep understanding of the template mechanism of the content management system (CMS).In the daily use and deep customization of AnQiCMS (AnQiCMS), the flexibility and efficiency of the template are one of the key factors determining the success or failure of website operations.

2025-11-07