When developing website templates for AnQiCMS, we often need to handle various data and decide the display method of the content based on these data.With the complexity of website functions, the logic in templates sometimes becomes difficult to understand and maintain.withThe tag is one of the practical tools that can significantly improve the readability and control of the template.

withTag: A powerful tool for defining temporary variables in templates.

withLabels allow us to define temporary variables in specific areas of a template. Its core function is to provide a local scope for variables within a block of template code, so that these variables are only accessible within that block.{% with %}and{% endwith %}Tags between are valid. This locality greatly helps us manage data flow within templates, avoid conflicts in variable names, and make the code structure clearer.

Imagine if the result of some complex data or expression needs to be used multiple times in a small section of a template, or if specific context data needs to be passed to a public component, directly repeating this complex logic would make the template long and difficult to read.withThe appearance of labels is to solve such problems. It provides a graceful way to declare temporary variables, making subsequent logic more concise.

Why to usewithLabel? Enhance template development efficiency and maintainability

  1. Enhance code readability and neatness:When a complex variable name or expression appears multiple times in a logic segment, assigning it to a concise temporary variable can make the code intent clearer and subsequent maintenance easier. For example, if a data obtained through multiple relationships needs to be used frequently, such asarchive.Category.TitleEnglish definition: Temporary defined ascurrentCategoryTitleMake the code clear at a glance, reduce misunderstandings.
  2. Optimize data processing process:Some data may need to go through a series of calculations or formatting to obtain the final result. If this result is used multiple times within a local range,withLabel it as a temporary variable to avoid repeated calculations, which can improve the efficiency of template rendering, especially when dealing with large amounts of data or complex calculation logic.
  3. Enhance template modularization capability: withTags andincludeThe combination of labels is one of the most common application scenarios.When we introduce a public template fragment (such as header navigation, sidebar widgets) we often need to pass some specific data to this fragment.withTags allow us to:{% include "partial/header.html" with title="页面标题" keywords="SEO关键词" %}The form, for providing the required environment variables to the imported template, thus realizing data transmission between template components, making the components more general and reusable.
  4. Refined content display logic:By using inwithDefine boolean values, counters, or other status variables in tags, allowing us to control more flexiblyifdetermineforLoop logic, for example, you can temporarily set aisFeaturedAn example of the basic usage of the tag to control the highlighting of a specific content, or to calculate and store an intermediate result in a loop to affect the display of subsequent elements.

withAn example of the basic usage of the tag

withThe usage of labels is very intuitive, it always{% with %}and ends with{% endwith %}ends, clearly defining the scope of variables.

Define a single or multiple temporary variables:You can inwithDefine variables directly within a label, like this:

{% with pageTitle="我的网站首页" %}
  <h1>欢迎访问 {{ pageTitle }}</h1>
{% endwith %}

If you need to define multiple variables, just separate them with spaces withinwithtags, using thekey=valueformat:

{% with pageTitle="我的网站首页" siteSlogan="用内容连接世界" %}
  <h1>{{ pageTitle }} - {{ siteSlogan }}</h1>
{% endwith %}

Please note that these variablespageTitleandsiteSloganare only valid within{% with %}and{% endwith %}between is valid. Once they leave this block, they are no longer available.

CombineincludeTag to pass data:As mentioned earlier,withTags play a huge role when introducing public templates. Assuming yourpartial/header.htmlneed atitlevariables andkeywordsVariables to build pages.<head>Part:

{# partial/header.html 中的内容 #}
<head>
  <title>{{ title }}</title>
  <meta name="keywords" content="{{ keywords }}">
</head>

In the main template, import and pass data, just write the variable name and its corresponding value inwithParameters:

{% include "partial/header.html" with title="安企CMS官网" keywords="安企CMS,内容管理系统,GoLang" %}

This is,partial/header.htmlIt will use the values you pass.titleandkeywordsThe value is rendered without affecting the同名 variables of the external template.

Actual application cases

Suppose you are developing an article detail page, you need to display the article's category name and publication year at the top of the page.This information may come from a complex object path or may require additional formatting.with

Article category: {{ categoryName }} | Published