How does the AnQiCMS template organize common code (such as header and footer)?

Calendar 👁️ 55

As an experienced security CMS website operator, I know that efficient content management cannot do without a well-organized and easy-to-maintain template system.In AnQi CMS, to better manage the public code of the website, such as the header (Header) and footer (Footer), the system provides a clear template organization structure and powerful template engine features.This not only improves the development efficiency, but also ensures the consistency and maintainability of the website content.

AnQiCMS template structure overview

The Anqi CMS template system uses a syntax similar to the Django template engine, with.htmlas the file extension for template files, and are stored uniformly in the root directory./templateIn the template folder. Static resources such as styles, JavaScript scripts, and images related to the template are placed independently in/public/static/The catalog has effectively separated content from the presentation layer code. This layered design is the foundation for the long-term stable operation of the website.

The core of organizing public code:bash.htmlwithpartial/Table of contents

In AnQi CMS template design, the organization of common code follows a straightforward and efficient pattern. The basic structure commonly used by all templates, such as the overall framework of the website and the header and footer that each page needs to inherit, is usually placed centrally.bash.htmlThis file. This file can be considered as the 'master' or 'layout file' of a website template, defining the overall structure of the page and reducing the writing of repeated code.

exceptbash.htmlThis overall layout file, Anqi CMS also encourages its usepartial/The directory to store code snippets.These code snippets are typically independent, reusable UI components or content modules on a website, such as sidebars, breadcrumb navigation, copyright information blocks, ad spaces, or specific form components.Separate these small blocks of code into separate files, which not only makes the template structure clearer, but also facilitates flexible referencing and management across different pages or templates.bash.htmlandpartial/Directories are the foundation of organizing public code

Template inheritance mechanism:extendsThe application of tags

The Anqi CMS template engine supports powerful template inheritance, which is mainly achieved throughextendsTag implementation.extendsTags allow developers to define a basic template (usually the one mentioned above)bash.htmlThis contains the common structure and placeholders of the website. Other page templates can inherit this base template and then useextendsthe tag to inherit this base template and then useblockRewrite or fill in the specific area defined in the base template.

For example,bash.htmlYou can define one{% block title %}{% endblock %}to place the page title, as well as one{% block content %}{% endblock %}to place the main content of the page. When a specific page template inheritsbash.htmlAt the same time, you only need to redefine these in your template fileblockYou can customize the content without rewriting the entire HTML structure of the page.This mechanism greatly improves the reusability of the template and ensures the consistency of the overall style of the website.

Code snippet reuse:includeFlexible use of tags

In addition to template inheritance,includeTags are another important way of code reuse in AnQi CMS.includeThe tag is used to embed another independent template file in the current template, usually those that do not contain the overall layout but provide specific functionality or display content, such aspartial/header.htmlorpartial/footer.html.

ByincludeLabels, you can import modular content such as headers, footers, navigation menus, sidebars, etc. in the form of independent files into any required template.includeTags also supportwithA parameter allows developers to pass specific variables to the included template, for example{% include "partial/header.html" with title="当前页面标题" %}. If you only want to pass variables and do not want to inherit all variables of the current template, you can useonlyParameters are limited. This flexibility allowsincludeto become a module-building, combinable template tool, effectively improving development efficiency and code readability.

Macros:macroachieve advanced reuse

For more complex code snippets that require parameterization and logical processing, the Anqi CMS template engine also providesmacro(Macro function) tag.The macro function is similar to a function in programming languages, which can define a reusable code logic in a template and accept parameters.This allows developers to create highly abstract and reusable UI components, such as a render function for an article list item with custom parameters.includethe same, saved to a separate file and throughimportLabel introduction and usage. This advanced reuse method provides senior operators with greater customization space and code organization capabilities.

Summary

Anqi CMS passedbash.htmlAs the skeleton file of the website,partial/The directory stores reusable code snippets and combinesextends/includeandmacroThe powerful template engine tag constructs a set of efficient, flexible and easy to maintain public code organization system.This makes the management of the website header, footer, and other common modules simple and organized, greatly improves the efficiency of content publishing and website optimization, and lays a solid foundation for subsequent secondary development and feature expansion.


Frequently Asked Questions (FAQ)

Question: In Anqi CMS,bash.htmlandpartial/header.htmlWhat is the difference?

Answer:bash.htmlIt is generally considered the overall layout file or "master" of a website, defining the basic HTML structure of the entire page, for example<html>/<head>/<body>Tags and the approximate division of the page, including areas that can be overwritten by sub-templates.blockAnd.partial/header.htmlThis is a more specific code snippet, it only contains the HTML content of the page header (Header), such as the website logo, main navigation menu, etc., this fragment is usually passed throughincludeTags are introduced tobash.htmlor other basic layout files. In simple terms,bash.htmlis a framework,partial/header.htmlis a pluggable component of the framework.

Question: Which should I choose to useextendsDo you want to perform template inheritance, orincludeuse common code snippets?

Answer: It depends on the nature of the code you want to reuse. If you need a page to have a unified overall structure, and some areas need to be customized or replaced based on different page content, thenextends(Template inheritance) is the better choice.It provides a "is-a" relationship, the child template inherits the skeleton of the parent template.includeIs a better choice.It provides a "has-a" relationship, embedding a component into the current template.extendsDefined, while the specific modules are passed throughincludeIntroductionpartial/Code snippets under the directory.

Question: How toincludeUse the variables of the current page in the public code snippet included?

Answer: When using{% include "partial/your_snippet.html" %}when introducedyour_snippet.htmlBy default, it will inherit all variables from the current template context. This means you can directly use them without any additional operationyour_snippet.htmlUse the variable of the current page. If you want to control the passing of variables more accurately, you can usewithparameters to explicitly pass specific variables, such as{% include "partial/your_snippet.html" with page_title=current_page.title %}. If you only want to passwithVariables specified in the context without inheriting any other context variables can be added inwiththe parameter after addingonlykeywords.

Related articles

What do the two states represent for the `status` field in `config.`?

In AnQiCMS (AnQiCMS) template management system, the `config.` file plays a crucial role, acting like an ID card for the template, recording all basic information and current status of the template.The `status` field is the key indicator used by the system to identify whether a template is activated and in use.This field has only two possible values, each representing different meanings and uses, which are crucial for the normal operation of the website and the flexible management of templates.### Status 1: `status: 0`

2025-11-06

How to configure the display mode of the `template_type` field in `config.`?

As an experienced CMS website operation personnel, I am well aware of the importance of template configuration for website presentation and user experience.Today, let's delve into the `template_type` field in the `config.` file, which finely controls the display mode of your website template.In AnQi CMS, each template package core includes a configuration file named `config.`.This file is the 'identity certificate' of the template, which defines the name, version, author, and other basic information of the template. Among them

2025-11-06

When creating a new template, what are the required and optional fields in the `config.` file?

In Anqi CMS, creating a new website template is a key step to achieving personalized content display.The core of each template includes a `config.` configuration file, which is like the template's 'ID card', declaring the basic information, functional characteristics, and management status to the system.Familiarize yourself with the structure and field functions of the `config.` file, which is crucial for website operators and template developers, as it ensures that the template is correctly identified, managed, and applied by the system.

2025-11-06

Where is the root directory of AnQiCMS template located?

As a senior security CMS website operator, I fully understand the core position of templates in website construction and content presentation.The template not only determines the visual style of the website, but also directly affects user experience and the efficiency of content delivery.Accurately understand the storage location of the template file is the basis for carrying out any website customization and optimization work. ### The Root Directory Location of AnqiCMS Templates In the architecture design of AnqiCMS, all the core files of the website templates are stored in a clear root directory.

2025-11-06

How to create and reference reusable code snippets (such as sidebars) in AnQiCMS templates?

As an experienced CMS website operation personnel of a security company, I know that efficiency and maintainability are the key to website content management in daily work.AnQiCMS provides a powerful and flexible template system, where creating and referencing reusable code snippets is an important means to improve these aspects of efficiency.This not only reduces repetitive labor, but also ensures the consistency of the overall style and function of the website, and also lays a solid foundation for future content updates and website renovations.

2025-11-06

What is the naming convention for the default homepage template file in AnQiCMS?

As an experienced CMS website operation personnel of an enterprise, I know that the naming specification of template files is crucial for the smooth operation and efficient management of the website.A clear naming convention not only ensures that the system renders the page correctly, but also greatly improves team collaboration and post-maintenance efficiency.Below, I will elaborate on the naming rules of the default home page template files in Anqi CMS.In AnQi CMS, all template files are stored in the `/template` folder under the system root directory.Each independent website template will be in `/template`

2025-11-06

How to create default templates for the model home page, document detail page, and list page of AnQiCMS?

As an experienced security CMS website operator, I am well aware of the importance of a clear and efficient template system for website content management.AnQiCMS provides a highly flexible and customizable template system that allows us to easily create personalized page layouts based on different business needs.Mastering the creation method of its default template is the foundation for efficient operation and content optimization.This article will detail how to create default templates for the model homepage, document detail page, and list page of AnQiCMS, with instructions on the use of key tags

2025-11-06

What is the default naming rule for single page detail page templates in AnQiCMS?

As an experienced CMS website operator for an enterprise, I am well aware that the exquisite presentation of content lies in the flexible application of templates.AnQiCMS as an efficient and customizable content management system provides us with powerful tools to shape every detail of the website, including the layout and style of single-page detail pages.Understanding the template naming rules is the foundation for efficiently managing and optimizing website content.

2025-11-06