As an experienced website operations expert, I am well aware of the powerful potential of AnQiCMS (AnQi CMS) in content management and website optimization.The flexible content model and customizable fields provide great convenience for us to build highly personalized websites.However, how to elegantly handle these dynamically generated custom fields in template design, ensuring that they are displayed only when they exist or have values, is the key to improving the quality of the template and user experience.archiveParamsIn the tag, cleverly judge the existence and value of custom fields to achieve intelligent display of content.
Flexible application of AnQiCMS custom fields
One of the most prominent features of AnQiCMS is its highly flexible content model.Whether it is an article, product, case or any other type of business content, we can add a series of custom fields for each content model according to actual needs.For example, a "product" model may require fields such as "product model
The introduction of these custom fields greatly enriches the dimensions of the content, but also poses higher requirements for the development of front-end templates.We do not want to have empty titles or placeholders in the template. If a field is not filled in, it should not be displayed. This not only concerns aesthetics but also enhances the professionalism of the website.
archiveParams: The key to unlocking custom fields
In the AnQiCMS template system,archiveParams
archiveParamsThere are two main ways to obtain labels, which directly affect our strategy for determining whether a field exists or not:
sorted=true(Default):In this mode,archiveParamsIt will return an array of objects arranged in an order defined by the backend. Each element of the array is a containingName(field display name) andValueThe structure of the actual field value. This method is suitable for traversing all custom fields and displaying them uniformly.sorted=false:When we know the name of the custom field we want to query, we willsortedis set tofalseIt will be more convenient. At this point,archiveParamsIt will return an object similar to Go languagemapWe can directly access it through the "field call" (defined in the background content model settings) of the field,ValueFor example, if the custom field's call field isauthorwe canparams.author.ValueGet its value.
understand the difference between these two ways of acquisition, which is the premise for efficient field judgment.
How to determine if a field exists or has a value?
Now, let's take a specific look at how to make existence or value judgments in the two scenarios mentioned above. AnQiCMS (based on the Pongo2 template engine)ifThe logic judgment label handles empty values very intelligently, which provides us with great convenience. Inifthe condition, empty strings ("falseas well asnilNothing, etc. will be treated asfalse.
Scenario 1: Traverse all custom fields (sorted=true)
When we want to traverse all the custom fields of the document and only display those with content, sorted=trueThe pattern is preferred.
First, througharchiveParamsTag to get all parameters:
{% archiveParams params with sorted=true %}
{# 接下来,我们将遍历这些参数 #}
{% for item in params %}
{# 在这里进行判断,确保字段有实际值才显示 #}
{% if item.Value %}
<div class="custom-field-item">
<span class="field-name">{{ item.Name }}:</span>
<span class="field-value">{{ item.Value }}</span>
</div>
{% endif %}
{% endfor %}
{% endarchiveParams %}
Parsing:In this example,{% if item.Value %}It is the core of judgment. Ifitem.Valueis an empty string,nilOr if the template engine considers other values as 'empty', thenifThe condition will not be met, the HTML structure of this field will not be rendered, thereby avoiding the display of meaningless blank areas.
Scenario two: Accurately judge specific custom fields (sorted=false)
If your template design requires displaying a custom field with a specific name, such as "author", "product features", or "contact phone number", then usesorted=falseIt will be more direct.
Suppose we define a custom field in the background, whose 'called field' isproductFeaturesAnd its value is a multi-line text, each line representing a feature.
”`twig {% archiveParams customFields with sorted=false %}
{# 假设我们有一个名为 'productFeatures' 的自定义字段 #}
{% if customFields.productFeatures.Value %} {# 检查 'productFeatures' 字段是否有值 #}
<div class="product-features">
<h3>产品特点:</h3>
<ul>
{# 假设 productFeatures.Value 是一个多行文本,我们可以按行分割并显示 #}
{%