Manage content in the Anqi CMS, we often encounter such needs: we hope to combine multiple field values in a custom content model to form a more expressive or format-compliant text.For example, you may need to concatenate the "brand" and "modeladdfilter combined witharchiveParamsTags, can help us efficiently implement these operations.

Flexible content model witharchiveParamsapplication

One of the core strengths of AnQiCMS is its flexible content model.它允许我们为不同类型的内容(如文章、产品、活动等)定义专属的字段,而不是局限于固定的标题和正文。For example, for a "product

Access these custom fields in the template,archiveParamsTags are our powerful assistants. They can retrieve all custom parameters of the current document or a specified document. There are two ways to use them, which can be flexibly chosen according to your specific needs:

一种是当你知道具体的字段名称时,通过设置sorted=false来以键值对(map)的形式访问。这样,你就可以像访问对象属性一样,直接通过params.FieldName.ValueGet the value of a specific field, for exampleparams.Brand.Value. This way, the code will be more concise and intuitive when the field name is clear.

Another is the default case or when you need to iterate over all custom fields, setsorted=trueIt will return an ordered array of objects. Each array element includesName(field display name) andValue(Field value). You can use a loop to display these fields, but this is slightly less efficient when concatenating specific fields directly.

To efficiently concatenate values from different fields, we usually choosearchiveParams params with sorted=falsethe method, as it allows us to directly point to and retrieve the required field values.

addthe cleverness of the filter

Once we have passed througharchiveParams获取到自定义字段的值,接下来就是如何将它们拼接起来。AnQiCMS模板中的 Englishadd过滤器在此刻显得尤为实用。它就像编程语言中的 English+Operator, which can perform addition of numbers and concatenation of strings.It is especially worth mentioning that it can intelligently handle different types of data: if you try to add two numbers, it will perform mathematical operations; if you concatenate a string with other types of data, it will try to convert non-string types to strings before concatenation.If the conversion fails, it will elegantly ignore the parts that cannot be concatenated without interrupting the rendering of the entire template, which greatly enhances the robustness of the template.

addthe basic syntax of filters is{{ obj|add:obj2 }}where,objIs the original value.obj2Is the value to be added. This operation can be chained to concatenate multiple values and strings sequentially.

Practice: Efficient concatenation of field values

Let's look at several specific scenarios to see how toarchiveParamsandaddcombine filters to efficiently concatenate field values.

Scenario 1: Build product display titles

Assuming your product custom model includes the fields "Brand" and "Model

First, get these custom fields in the template:

{% archiveParams productInfo with sorted=false %}

Then, you can concatenate them like this:

<h2 class="product-title">
    {{ productInfo.Brand.Value | add: " - " | add: productInfo.Model.Value }}
</h2>

This code will first getBrandField value, then concatenate a static string " - " and then concatenateModelField value, finally forming a complete title, such as 'AnQiCMS - Advanced Edition'.

Scenario 2: Concatenating a prefixed unique identifier

If your product has a unique code consisting of "Product SKU (SKU)" and

{% archiveParams productInfo with sorted=false %}
<p class="product-code">
    {{ "产品编码:" | add: productInfo.SKU.Value | add: "-" | add: productInfo.ColorCode.Value }}
</p>

Here, we first concatenate a static string 'Product Code: ', then concatenate the SKU value, a hyphen, and the ColorCode value in sequence.

Scene three: Handling the concatenation of optional fields

In practical applications, some custom fields may not be required.If a field is empty, we do not want it to leave an extra connector in the concatenated result.ifFlexible handling with logical judgment labels.

If you wish to concatenate "CPU model" and "memory capacity", but one or both of these fields may be empty.We hope to display them only when they have values and to separate them with slashes.

{% archiveParams computerSpecs with sorted=false %}
<p class="computer-summary">
    {% set cpu = computerSpecs.CPUType.Value %}
    {% set ram = computerSpecs.RAMSize.Value %}

    {% if cpu and ram %}
        {{ cpu | add: " / " | add: ram }}
    {% elif cpu %}
        {{ cpu }}
    {% elif ram %}
        {{ ram }}
    {% else %}
        暂无配置信息
    {% endif %}
</p>

In this example, we first assign the field value to a temporary variablecpuandramImproved code readability. Then, byif-elif-elseStructure, judge if two fields exist, and concatenate or display separately according to the situation. In this way, even if a field is missing, the output result is still elegant and will not appear extra slashes.

Some suggestions

  • Keep field names clear:When defining custom fields in the background, use descriptive and easily understandable English names, so that they can be used in templates.archiveParamsBysorted=falseThe code will be more clear when accessed in this way.
  • UtilizesetTags:When your concatenation logic becomes complex, consider using{% set variable = value %}Labels store intermediate results in temporary variables, build the final string step by step, which can greatly enhance the readability and maintainability of the template.
  • Sufficient testing:Especially when it involves conditional judgment and concatenation of multiple data types, it is necessary to test your template in different data states, to ensure that the output meets expectations, and to avoid blank connectors or format errors.

Through the flexible use of AnQiCMS,archiveParamsTags andaddFilter, we can easily handle various custom content field concatenation requirements, making the website content display more dynamic and accurate.This not only improves the user experience, but also provides more possibilities for website SEO optimization, after all, clear and structured information is always more favored by search engines.


Common Questions and Answers (FAQ)

Q1:addCan the filter perform numerical operations in addition to string concatenation?A1: Yes,addThe filter is very intelligent. When you pass it two numbers, it performs mathematical addition. For example,{{ 5 | add: 2 }}will be displayed7When you mix numbers and strings, it will try to convert the number to a string before concatenating.

Q2: How to avoid extra concatenation symbols if the custom field to be concatenated may be empty?A2: This is a very common requirement in actual development. You can combine it with the AnQiCMS template.ifUse logical judgment tags to solve.Before concatenation, first check if the values of relevant fields exist. Only concatenate when fields have values, and add a connector when necessary.The scenario of 'Combining Optional Fields' provides a detailed example.

Q3:archiveParamsto get custom fields by tags.sorted=trueandsorted=falseWhat are the differences? Which one should I choose?A3:sorted=true(默认值)Will return an ordered array containing a custom field object, with each object havingNameandValueproperties. This method is suitable for iterating over all custom fields.sorted=falseReturns a key-value pair (map), you can directly access the value of a specific field by field name (such asparams.YourFieldName.Value). For efficient