AnQiCMS provides a powerful and flexible template system, allowing highly customizable display of website content.Its template tag syntax is based on the Django template engine and combines the ease of use of Blade templates, making it easy for users unfamiliar with the Go language to get started quickly.However, to fully exploit the potential of template tags and avoid common mistakes, it is crucial to understand their correct usage and potential pitfalls.

AnQiCMS template tag basics: Understanding core syntax and conventions

AnQiCMS template files are usually named with.htmlwith suffix, stored in/templatedirectory. The core syntax for handling data output and logic control in the template is double curly braces{{变量}}With a single curly bracket and a percentage sign{% 标签 %}.

  • Data output ({{变量}}): Used to display data directly, for example{{archive.Title}}It will output the title of the article. Variable names usually follow camel case naming conventions, for exampleId/Title/CreatedTimeetc.
  • Logical control ({% 标签 %}): Used to implement conditional judgments, loop traversals, file imports, and other functions. These tags must appear in pairs, with start and end tags, such as{% if 条件 %}...{% endif %}or{% for item in 列表 %}...{% endfor %}.

In actual use, template files will also cooperate/public/static/The directory contains CSS, JavaScript, images, and other static resources.To ensure that the template can be parsed correctly, all template files should be saved using UTF-8 encoding.

AnQiCMS also supports some default template file naming conventions, such as document lists can be named{模型table}/list-{分类id}.html, and single pages can be namedpage/{单页面id}.html. Understanding these conventions helps us better organize template files and achieve personalized display of specific page content.

Key strategies to avoid common errors.

To use AnQiCMS template tags correctly and avoid common problems, the following aspects are particularly worth paying attention to:

  1. Case sensitivity is strictly required: AnQiCMS template tags and variable names are case-sensitive. For example,{{archive.id}}and{{archive.Id}}are two different variables. The tag names likearchiveListandarchivelistAlso, do not mix them up. A tiny spelling error can cause the tag to be unrecognized or the data to not display normally.
  2. Correct closing of the tag: All logical control tags (such as{% if %}/{% for %}/{% archiveList %}Tags must have corresponding closing tags{% endif %}/{% endfor %}/{% endarchiveList %}Forgetting to close the tags is a common mistake made by beginners, which can lead to template parsing errors and even cause web pages to crash.
  3. Accurate passing of parameters: Many tags require parameters to specify what data to retrieve or how to process. For example,{% archiveDetail with name="Title" id="1" %},nameParameters specify the fields to retrieve,idThe parameter specifies the article ID. Different tags support different parameters, be sure to consult the relevant documents to ensure the correctness of the parameter name and value range. Pay special attention.moduleId/categoryId/type/limitThe usage of commonly used parameters.
  4. The replacement of old and new tags: AnQiCMS is inv2.1.1Version, the template tags were reconstructed, and the original tags were removed.articleandproductTags were unified and added.archiveLabel. This means that if your website has been upgraded from an old version or has referred to the old tutorial, it still usesarticleListorproductDetailtags, it will cause an error. It is imperative to use the newarchiveList/archiveDetailtags for the unifiedarchiveSeries tags.
  5. Matching data type and filter.: Sometimes, data formatting or processing is required when outputting data.
    • Timestamp conversion:CreatedTimeandUpdatedTimefields usually return Unix timestamps and need to bestampToDatetag to format them as readable dates such as{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}.
    • safe output of HTML content: Article details (archive.Content) categorization content (category.ContentFields such as ")"}In order to correctly parse HTML, you need to use|safeFilter, for example{{archive.Content|safe}}.
    • Markdown content renderingIf the content is written in Markdown, it will be automatically converted when the Markdown editor is turned on. To manually control the conversion, you can addrenderParameters, or use|renderFilter, for example{{archive.Content|render|safe}}.
    • Content extraction: For long texts, you can use|truncatechars:100(Character truncation) or|truncatewords:50(Cut by words) and other filters to truncate and add ellipses.
  6. Understanding based on context: Some tags (for example)archiveDetail/categoryDetail) are attempted to be automatically retrieved when no explicit parameters are specified.idortokenThis is used on the document detail page.archiveDetailIt will automatically retrieve the current document data, but when used on the homepage, it needs to be explicitly specifiedidto retrieve the data of a specific document
  7. Correctly refer to static resources and template pathsIn the template, when referencing static resources (images, CSS, JS), you should use{% system with name="TemplateUrl" %}tag to get the root path of the static resources of the current template, for example<link href="{% system with name="TemplateUrl" %}/css/style.css" rel="stylesheet">.includeWhen using other template fragments, the path should also be correct, for example{% include "partial/header.html" %}.

Practical Guide: The correct way to open common tags

AnQiCMS provides rich tags, the following lists some commonly used tag usage methods and points out the places that need attention:

  • Global information acquisition (system,contact,tdk):

    • {% system with name="SiteName" %}Get the website name. This type of tag is usually not requiredendTag, output the result directly.
    • {% contact with name="Cellphone" %}Get the contact phone number. You can customize the name for convenience when calling, such as{% contact myPhone with name="Cellphone" %}{{myPhone}}.
    • {% tdk with name="Title" siteName=true %}Get the page title and you can choose whether to append the website name.siteNameThe parameter only applies toTitleTake effect.
  • Content list and details (archiveList,archiveDetail,categoryList,categoryDetail,pageList,pageDetail):

    • Article/Product list:{% archiveList archives with moduleId="1" type="page" limit="10" %}
      • moduleId: Specify the content model ID (such as1For article,2For product). This is a very important filtering condition.
      • typeIt can be.list(Fixed quantity list) orpage(Paging list).
      • limit: Limit the number of displayed items. Fortype="page", needs to be matched{% pagination %}label usage
      • categoryId: Specify the category ID. Multiple IDs are separated by commas.
      • Use within the loop,forTag traversalarchivesthrough.{{item.Title}}/{{item.Link}}etc. to obtain fields.
    • article/product details