English CMS template: Exploring the way to check the existence of key-value pairs and members in structures
As an experienced website operations expert, I know how important it is to operate data flexibly and effectively in daily content management and website maintenance.In such a powerful content management system based on Go language as AnQiCMS, the data processing capability at the template level directly affects the display effect and development efficiency of the front-end pages.Today, let's delve into a common template operation requirement: how to check if a key (key) exists in a map (key-value pair) or a struct (structure).
In the template engine of AnQiCMS, we have a variety of methods to achieve this goal, each with its own focus, meeting the needs of different scenarios.We will delve into the two aspects of the built-in operators and powerful filters of the template engine in detail.
Template in the “in” operator: Intuitive member detection
Firstly, the template engine of Anqi CMS provides a very intuitive and easy-to-understandin
When used for lists or arrays,inThe operator checks if a list contains a certain value. For example, if you have a list containing multiple strings and you want to know if it contains "SEO optimization
{% if "SEO优化" in 网站优势列表 %}
<p>我们的安企CMS在SEO优化方面表现出色!</p>
{% endif %}
While facingmap(key-value pair) orstruct(struct) wheninoperators can checkkey name(key)exists. This is very practical for dynamically displaying content or deciding whether to display a module based on the existence or absence of data structure.
For example, in the template design of Anqi CMS, we often encounterarchive(document) object, which may contain various fields. Suppose we want to know ifarchivean object contains a field namedViewsThe field (usually representing page views), we can judge it like this:
{% if "Views" in archive %}
<p>浏览量:{{ archive.Views }}</p>
{% else %}
<p>此文档暂无浏览量数据。</p>
{% endif %}
For example, you may have one passed from the backgroundintmap(integer key-value pair), want to check the number5Is it present as a key:
{% if 5 in simple.intmap %}
<p>键5存在于intmap中。</p>
{% endif %}
This method is concise and clear, especially suitable inifDirect conditional judgment in the statement.
A more elegant choice:containFilter
ExceptinOperator, the CMS also provides a more powerful and flexiblecontainFilter, it can be combined with other operators in the form of a filter chain to achieve more complex data processing.containThe filter can not only judge whether a substring is contained in a string, but also efficiently check if a certain value exists in an array,In a key-value pair (map) or a structure (struct), does the specified key name exist?.
containWhen the filter is used, it will return a boolean value (TrueorFalseThis makes it very suitable for storing the check results in variables for subsequent use in multiple logical branches.
Let's understand this through a specific example.containFilter application in checking map or struct key names:
Assuming you have a namedwebInfoThe key-value pair contains the website's title (Title), keywords (Keyword), and description (Description). Do you want to checkTitleDoes this key exist:
{% set webInfo = {'Title': '安企CMS', 'Keyword': 'AnQiCMS', 'Description': '免费建站系统'} %}
{% if webInfo|contain:"Title" %}
<p>网站信息中包含Title键:{{ webInfo.Title }}</p>
{% else %}
<p>网站信息中不包含Title键。</p>
{% endif %}
The advantage of this writing style is,containThe boolean value returned by the filter can be directly{% set %}Captured by the tag, assigned to a new variable, which improves the readability and reusability of the code:
{% set webInfo = {'Title': '安企CMS', 'Keyword': 'AnQiCMS', 'Description': '免费建站系统'} %}
{% set hasTitleKey = webInfo|contain:"Title" %}
{% if hasTitleKey %}
<p>我们确定网站信息中包含Title键!</p>
{% else %}
<p>Title键缺失,请检查网站配置。</p>
{% endif %}
containThe filter is also powerful when dealing with strings and arrays. For example, check if a piece of text contains a certain word:
{% set welcomeText = "欢迎使用安企CMS(AnQiCMS)" %}
{% if welcomeText|contain:"CMS" %}
<p>欢迎语中提到了CMS。</p>
{% endif %}
Or check if an element exists in a list (slice):
{% set features = ["多站点管理", "灵活内容模型", "SEO工具"] %}
{% if features|contain:"SEO工具" %}
<p>安企CMS确实提供了强大的SEO工具。</p>
{% endif %}
When to use which? Scene analysis
Then,inOperators andcontainFiltering between, how should we choose?
For simple conditional judgments:If you just need to check if a key exists in a statement without storing the result or performing other chained operations,
{% if ... %}theninThe operator is usually a more concise and intuitive choice.{% if "author" in archive %} ... {% endif %}For cases where the result needs to be assigned to a variable or perform complex filtering chainsIf you wish to store the check result in a variable, or need to perform other data filtering and processing before/after the check,
containThe filter has more advantages. Filters usually provide higher flexibility.{% set hasCustomField = archive.Extra|contain:"custom_field_name" %} {% if hasCustomField %} ... {% endif %}For uniform syntax checkingIf you want to use a unified filter syntax to check for the existence of members in strings, arrays, or key-value pairs, then
containThe filter would be a better choice, as it provides a consistent interface.
This design of the AnQi CMS template engine provides strong flexibility for website operators and developers, allowing us to choose the most suitable tools to efficiently process data based on specific data types and business logic.
Common Questions (FAQ)
I can use
not inor!operator to check for non-existent keys?Of course you can. In the template engine of AnQi CMS, you can usenot into directly express the logic of 'not included', or ininThe result of the operator can be prefixed with!(Logical NOT) to achieve. For example,{% if "author" not in archive %}or{% if not ("author" in archive) %}All of them can be checkedauthorWhether the key exists. Similarly,containThe filter returns a boolean value, you can directly negate it:{% if not (webInfo|contain:"Title") %}.How do I know if the key exists not only to get its value but also to get its position in the array or string?如果您需要获取某个值在字符串或数组中首次出现的位置,可以使用English CMS提供的
index过滤器。它会返回该值从0The starting index position, if not found, it returns-1Please note,indexThe filter is mainly used for locating the values of strings and arrays, not for locating the keys of key-value pairs. For example:{{ "欢迎使用安企CMS"|index:"CMS" }}.Custom content model fields can also be used
inOperator orcontainFilter check?Yes, the fields of the custom content model in AnQi CMS are usually present as properties of document or category objects (structs in Go language). Therefore, you can check these fields just like any other struct fields, usinginOperator (for example{% if "customFieldName" in archive %}) orcontainfilters (for example{% if archive|contain:"customFieldName" %}) to determine whether these custom fields exist. This provides great convenience for dynamically displaying content based on the presence or absence of custom fields.