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.