Secure CMS Template: Exploring the checking of key-value pairs and member existence 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 as AnQiCMS, which is developed based on Go language, the data processing capability at the template level directly affects the display effect and development efficiency of the front-end page.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 (struct).

In AnQiCMS template engine, we have a variety of methods to achieve this goal, each with its own focus, meeting the needs of different scenarios.We will detail the two aspects of the built-in operators and powerful filters of the template engine.

In the template, the “in” operator: intuitive member detection

First, the Anqi CMS template engine provides a very intuitive and easy-to-understandinAn operator that allows us to check if an element belongs to a set in a conditional statement.This "collection" can be a list (such as a slice or array in Go language), or a key-value pair (map) or a struct.

When used with lists or arrays,inThe operator checks if a list contains a certain value. For example, if you have a list containing multiple strings and want to know if it contains "SEO optimization", you can write it like this:

{% if "SEO优化" in 网站优势列表 %}
    <p>我们的安企CMS在SEO优化方面表现出色!</p>
{% endif %}

And when we face ismap(key-value pair) orstruct(struct) wheninThe operator can checkkey nameDoes the (key) exist. This is very useful for dynamically displaying content, or determining whether to display a module based on the existence of the data structure.

For example, in the AnQi CMS template design, we often encounterarchive(document) object, which may contain various fields. Suppose we want to know somearchiveWhether the object contains a 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 might have one passed from the backgroundintmap(integer key-value pair), want to check the number5Does it exist as a key:

{% if 5 in simple.intmap %}
    <p>键5存在于intmap中。</p>
{% endif %}

This method is concise and clear, especially suitable forifdirect conditional judgment in statements.

A more elegant choice:containFilter

exceptinOperator, Anqi CMS also provides a more powerful and flexible feature.containFilter, 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 whether a value exists in an array, andDoes the key exist in the key-value pair (map) or structure (struct)?.

containThe filter returns a boolean value when used,TrueorFalseThis makes it very suitable for storing check results in variables, for subsequent multiple logic branches.

Let's understand through a specific example.containThe application of filters in checking map or struct key names:

Suppose you have a variable 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 %}Labeled and assigned to a new variable, improving 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 handling 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? Scenario analysis

Then, ininOperator andcontainBetween filters, how should we choose?

  1. For simple conditional judgmentsIf you just need to{% if ... %}quickly check if a key exists in a statement without storing the result or performing other chained operations, theninOperators are usually a more concise and intuitive choice.

    {% if "author" in archive %} ... {% endif %}
    
  2. For cases where the result needs to be assigned to a variable or for complex filtering chains: If you wish to store the check results in a variable, or need to perform other data filtering and processing before/after the check, thencontainThe filter has more advantages. Filters usually provide greater flexibility.

    {% set hasCustomField = archive.Extra|contain:"custom_field_name" %}
    {% if hasCustomField %} ... {% endif %}
    
  3. For unified syntax checking: If you want to check the existence of members in strings, arrays, or key-value pairs using a unified filter syntax, thencontainThe 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 tool to efficiently process data based on specific data types and business logic.


Frequently Asked Questions (FAQ)

  1. Can I usenot inor!Are you using the operator to check if a key does not exist?Of course, you can. In AnQi CMS template engine, you can usenot into express the logical 'not included' directly, or ininAdd the result of the operator before!To implement (logical NOT). For example,{% if "author" not in archive %}or{% if not ("author" in archive) %}Can be checkedauthorKey exists. Similarly,containThe filter returns a boolean value, you can directly negate it:{% if not (webInfo|contain:"Title") %}.

  2. How can I find not only if the key exists but also get the position of its value in an array or string?If you need to get the first occurrence position of a value in a string or array, you can use the Anqi CMS providedindexfilter. It will return the value from0The starting index position, if not found then return-1. Please note,indexThe filter is mainly used for locating values of strings and arrays, not for locating keys of key-value pairs. For example:{{ "欢迎使用安企CMS"|index:"CMS" }}.

  3. fields of a custom content model can also be usedinor operatorcontainfilter check?Yes, the fields of the custom content model in AnQi CMS are usually present as attributes of document or category objects (structs in Go language). Therefore, you can check them as you would check any struct field.inOperator (for example{% if "customFieldName" in archive %}orcontainfor example, a filter (such as{% 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.