How to use the template inheritance and reference feature of AnQiCMS to build reusable display modules?

Calendar 👁️ 66

When using AnQiCMS to build a website, we often encounter such situations: each page of the website has common header navigation, footer information, or a module (such as a sidebar, article recommendation list) that needs to be displayed in multiple places, but the content or style may be slightly different.If you copy and paste this code every time, it is not only inefficient, but also once it needs to be modified, it has to be adjusted one by one on all related pages, which is very easy to make mistakes and difficult to maintain.

AnQiCMS fully considers this requirement, it built-in strong template inheritance and reference features, allowing us to build websites as efficiently and flexibly as stacking building blocks, ensuring the reusability of the code and the maintainability of the website.Master these features, and you can easily handle the template, making the development and operation of the website smoother.

1. Why do we need reusable display modules?

The importance of reusable modules in website content operation and development is self-evident:

  1. Improve efficiency:Avoid rewriting the same HTML structure or functional code, which greatly shortens development time.
  2. Maintain consistencyThrough centralized management of public modules, it can ensure the unity of style and function of the website's various parts, improving user experience.
  3. Simplify maintenanceWhen the website design or function needs adjustment, only one core module needs to be modified, and all the places referencing the module will be updated synchronously, avoiding cumbersome manual modifications and reducing the risk of errors.
  4. Easy to expandCan easily insert new feature modules or replace old modules in the existing structure, enabling the website to quickly adapt to business changes.

AnQiCMS uses a syntax similar to the Django template engine, which makes template inheritance and referencing very intuitive and powerful. Variables are usually used{{变量}}The double curly braces form, while logical control labels, such as conditional judgments and loops, use{% 标签 %}The single curly brace with a percentage sign form, and most logical labels require corresponding end tags, such as{% if ... %} ... {% endif %}.

Second, template inheritance: building the skeleton of a website

Template inheritance is the core function for building the overall layout of a website, which allows you to define a basic “master” template that includes common website structures (such as headers, footers, and sidebars), and then let other page templates inherit this master template.

  1. core concept: Template inheritance is like making a "universal draft" for your website. You can draw all the common parts on this draft and leave some "blank areas" (namelyblockLabel), let the child template inherit it to fill in the blank areas, or choose to use the default content preset in the template.This way, all sub-pages will have the same skeleton, but the specific content can be different.

  2. How to useextendsandblockTag:

    • Define a master template (base.html): Typically, we would create one in the template directorybase.htmlThe file, as the master of the website. In this file, you can define the HTML structure of the entire page, include CSS and JavaScript files, and use{% block 块名称 %}{% endblock %}Label to define an area that can be overlaid by child templates.
      
      <!DOCTYPE html>
      <html lang="zh-CN">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          {# 头部标题,可被子模板覆盖 #}
          {% block title %}
              <title>我的网站 - AnQiCMS</title>
          {% endblock %}
          <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
          {% block head_extra %}{% endblock %} {# 预留给子模板添加额外的head内容 #}
      </head>
      <body>
          <header>
              {% include "partial/header.html" %} {# 头部导航,通常作为一个独立模块引用 #}
          </header>
      
          <div class="main-content">
              <aside class="sidebar">
                  {% block sidebar %}
                      {# 默认侧边栏内容,子模板可覆盖 #}
                      <p>这里是默认的侧边栏内容。</p>
                      {% include "partial/latest_articles.html" %} {# 推荐文章模块 #}
                  {% endblock %}
              </aside>
      
              <main class="content-area">
                  {% block content %}
                      {# 页面主要内容区域,子模板必须填充 #}
                      <p>欢迎来到我的网站!</p>
                  {% endblock %}
              </main>
          </div>
      
          <footer>
              {% include "partial/footer.html" %} {# 底部信息,通常作为一个独立模块引用 #}
          </footer>
          {% block body_extra %}{% endblock %} {# 预留给子模板添加额外的body内容,例如JS脚本 #}
      </body>
      </html>
      
    • Inherit master template (index.htmlor other pages): The first thing you need to do in the child template is to use{% extends '母版文件路径' %}Use the tag to declare which master it inherits.Please note,extendsThe tag must be the first tag in the sub-template.Then, you can use the same named{% block 块名称 %}tag to fill or override the reserved areas in the master.
      
      {% extends 'base.html' %} {# 声明继承 base.html #}
      
      {% block title %}
          <title>首页 - 我的网站</title> {# 覆盖母版中的 title 块 #}
      {% endblock %}
      
      {% block content %}
          <h2>欢迎阅读我们的最新文章</h2>
          {% archiveList articles with type="list" limit="5" %}
              <ul>
              {% for item in articles %}
                  <li><a href="{{ item.Link }}">{{ item.Title }}</a> - {{ stampToDate(item.CreatedTime, "2006-01-02") }}</li>
              {% endfor %}
              </ul>
          {% endarchiveList %}
      {% endblock %}
      
      {% block sidebar %}
          {# 完全覆盖母版侧边栏,只显示分类导航 #}
          <h3>文章分类</h3>
          {% categoryList categories with moduleId="1" parentId="0" %}
              <ul>
              {% for category in categories %}
                  <li><a href="{{ category.Link }}">{{ category.Title }}</a></li>
              {% endfor %}
              </ul>
          {% endcategoryList %}
      {% endblock %}
      
      By this means,index.htmlNo need to repeat writing anymore<head>/<footer>Common code, just pay attention to the unique content area.

Third, template reference: insert independent code snippets

Template references are applicable to those independent code snippets that need to be reused in multiple pages or different positions on the same page, such as navigation menus, ad spaces, social sharing buttons, and so on.They are usually complete, self-contained modules.

  1. core concept: Template references are like the 'Legos' of a website. Each block is a complete, functional module that you can insert anywhere as needed, and even if the same block is inserted in multiple places, they are independent and do not affect each other.

  2. How to useincludeTag:{% include "代码片段文件路径" %}The tag can directly insert the content of another template file at the current position.

    • Define a code snippet (partial/header.html):partial/header.htmlAssuming
      
      <nav class="top-nav">
          <a href="/">首页</a>
          <a href="/about">关于我们</a>
          <a href="/contact">联系方式</a>
          {# 使用navList标签获取动态导航菜单 #}
          {% navList main_navs %}
              {% for item in main_navs %}
                  <a href="{{ item.Link }}">{{ item.Title }}</a>
              {% endfor %}
          {% endnavList %}
      </nav>
      
    • Refer to the code snippet: Inbase.htmlOr any other place where navigation needs to be displayed, use it directlyinclude:
      
      <header>
          {% include "partial/header.html" %}
      </header>
      
    • Handle optional reference (if_exists): If you are unsure

Related articles

How to define and use variables in AnQiCMS to display data flexibly in templates?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template function is the key to building a personalized website.In templates, defining and using variables flexibly allows us to easily handle and display various types of data, whether it's article titles, categorization information, or custom field content. All of this can be dynamically called through variables, making the website full of vitality.We will delve deeper into how to define, obtain, and use variables in AnQiCMS templates, helping everyone build and maintain websites more efficiently.

2025-11-08

How to format a timestamp in AnQiCMS template and display it in a specified date format?

In AnQiCMS template, processing timestamps and displaying them in a specific date format is a common requirement in content operation.No matter whether you need to display the publication time of the article or the update date of the product, understanding how to flexibly format time will greatly enhance the expressiveness of the website content.AnQiCMS provides simple and powerful template tags, making this process easy and convenient.

2025-11-08

How does the AnQiCMS template loop through arrays or objects to display list data?

In the Anqi CMS template system, the core of looping through arrays or objects and displaying list data lies in understanding its Django-like template syntax, especially the use of the `for` loop tag.This mechanism makes the presentation of dynamic content intuitive and efficient, whether it is for article lists, category navigation, or product displays, it can adapt flexibly. ### Core Mechanism: The Use of `for` Loops Data traversal in Anqi CMS templates mainly relies on `{% for ...in ...

2025-11-08

How to implement conditional logic in AnQiCMS to control the display of content?

In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting various business needs.This article will delve into how to make use of the template engine features in AnQiCMS, controlling the display of website content precisely through conditional logic judgments.

2025-11-08

How to display different content and manage interfaces for multiple sites in the AnQiCMS website?

AnQiCMS, with its powerful multi-site management functions, provides an efficient and flexible solution for operators with multiple brands, sub-sites, or content branches.It allows you to easily create, manage, and display multiple independently operated websites under the same core system, greatly enhancing the efficiency of content operation.

2025-11-08

How to configure and display the contact information of the AnQiCMS website, such as phone number, address, and social media?

The contact information of the website is crucial for establishing a connection and providing support to users.AnQiCMS (AnQiCMS) provides an intuitive and flexible way to manage and display this important information, whether it's phone numbers, addresses, or various social media platform links, all can be easily achieved. ### Configure the website contact information in the background To configure the website contact information, you can first log in to the Anqi CMS backend management interface.In the left navigation bar, find and click "Background Settings", then select "Contact Information Settings".Here is a collection of all the contact information you may need to display

2025-11-08

How to display the details and list of a custom single page in AnQiCMS?

In AnQiCMS, the Single Page (Page) feature provides the website with the powerful ability to display independent and fixed content, such as 'About Us', 'Contact Information', 'Company Profile', and other pages.These pages usually do not belong to a specific category, but exist independently, carrying important corporate information or brand stories.Efficiently create, manage, and flexibly display the details and lists of these single pages on the website, which is an indispensable part of content operation.

2025-11-08

How to display the banner image list on AnQiCMS template?

The visual elements of a website are crucial for attracting visitors and conveying brand information, and the Banner image list is one of its core components.AnQiCMS as a powerful content management system provides a flexible way to display these banner images on your website template, whether it be for the homepage slideshow, category page feature images, or single-page promotional images, all can be easily realized through concise template tags.

2025-11-08