When developing website templates on AnQiCMS, we often need to handle various data and decide the display method of the content according to this data.With the complexity of website functions, the logic in templates sometimes becomes difficult to understand and maintain.At this point, it is particularly important to master some advanced template tag usage, andwithTags are one of the practical tools that can significantly enhance the readability and control of templates.
withTags: a powerful tool for defining temporary variables in templates.
withTags allow us to define temporary variables within specific areas of a template. Its core function is to provide a local scope variable for a block of template code, and these variables are only valid within the{% with %}and{% endwith %}Labels between are valid. This locality greatly helps us manage the data flow in templates, avoiding conflicts in variable names and making the code structure clearer.
Imagine if the result of some complex data or expression needs to be used multiple times in a small segment of the template, or needs to pass specific contextual data to the public components introduced, directly repeating this complex logic will 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 the subsequent logic more concise.
Why to usewithLabel? Enhance template development efficiency and maintainability
- Improve code readability and cleanliness:When a complex variable name or expression appears multiple times in a piece of logic, assigning it to a concise temporary variable makes the code intent clearer and easier to maintain. For example, if you need to use a data obtained through multiple layers frequently, such as
archive.Category.TitleDefine it temporarily ascurrentCategoryTitleIt makes the code easy to understand, reducing misunderstandings. - Optimize the data processing process:Some data may need to be calculated or formatted in a series of steps to get the final result. If this result is used multiple times within a local scope, through
withLabel it and store it as a temporary variable to avoid repeated calculations, thereby improving template rendering efficiency, especially when dealing with large amounts of data or complex calculation logic. - Enhance template modularization ability:
withwith the tag andincludeThe combination of labels is one of the most common application scenarios.When introducing a common template fragment (such as header navigation, sidebar widgets) often need to pass some specific data to this fragment.withLabels allow us to use:{% include "partial/header.html" with title="页面标题" keywords="SEO关键词" %}The form, provides the required environment variables to the template being introduced, thereby enabling data transmission between template components, making the components more generic and reusable. - Refined content display logic:By adding in
withDefine boolean values, counters, or other status variables in tags, and we can control more flexiblyifJudgment orforLoop logic, for example, you can temporarily set aisFeaturedVariables to control the highlight display of a specific content, or to calculate and store an intermediate result in a loop to affect the display of subsequent elements.
withBasic usage example of the tag.
withThe use of labels is very intuitive, it always ends with{% with %}and ends with{% endwith %}which clearly defines the scope of the variable.
Define a single or multiple temporary variables:You canwithDefine variables directly within the label, like this:
{% with pageTitle="我的网站首页" %}
<h1>欢迎访问 {{ pageTitle }}</h1>
{% endwith %}
If you need to define multiple variables, just separate them inwiththe tag with spaces and usekey=valuethe format as follows:
{% with pageTitle="我的网站首页" siteSlogan="用内容连接世界" %}
<h1>{{ pageTitle }} - {{ siteSlogan }}</h1>
{% endwith %}
Please note, these variablespageTitleandsiteSloganonly{% with %}and{% endwith %}Valid between. Once they leave this block, they are no longer available.
CombineincludeTags pass data:As mentioned earlier,withTags play a huge role when introducing public templates. Suppose yourpartial/header.htmlneed atitlevariables andkeywordsVariables to build pages<head>Part:
{# partial/header.html 中的内容 #}
<head>
<title>{{ title }}</title>
<meta name="keywords" content="{{ keywords }}">
</head>
Introduce and pass data in the main template, just write the variable name and the corresponding value inwiththe parameters:
{% include "partial/header.html" with title="安企CMS官网" keywords="安企CMS,内容管理系统,GoLang" %}
Thus,partial/header.htmland it will use the one you passtitleandkeywordsThe value is rendered without affecting the同名 variable in the external template.
Actual application cases
Assuming you are developing an article detail page, you need to display the 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.You can use it like thiswithLabel to simplify logic:
“`twig
{% archiveDetail currentArchive %} {# Assuming this tag has assigned the current article data to the archive variable #}
{% with categoryName=currentArchive.Category.Title publishYear=stampToDate(currentArchive.CreatedTime, “2006”) %}
Article category: {{ categoryName }} | Publish