How to get the first element/character of an array or string in AnQi CMS template?

Calendar 👁️ 65

In the process of website template development, it is often encountered that it is necessary to extract the first element or character from a data collection (whether it is an array, slice, or string).AnQiCMS uses syntax similar to the Django template engine, providing rich tags (Tags) and filters (Filters), allowing us to flexibly and efficiently handle data.

This article will introduce in detail how to easily obtain the first element/character of an array or string using these powerful tools in the AnQiCMS template.


1. UsefirstFilter: Directly retrieve the first element or character

AnQiCMS provides a very intuitive and concisefirstA filter that is used specifically to get the first element/character of an array, slice, or string.When you know that the data is an iterable collection or a string and you only need the first part, this is the most recommended method.

firstThe filter automatically identifies the type of input data:

  • For arrays or slicesIt will return the first element of the array.
  • For stringIt will return the first character of the string. It is worth mentioning that AnQiCMS'sfirstThe filter can correctly handle Chinese characters and will return the first Chinese character.

Usage:

You just need to pass the variable you want to process through the pipe symbol|Connected tofirstthe filter.

{# 假设我们有一个字符串变量 #}
{% set myString = "AnQiCMS 内容管理系统" %}
<p>字符串的第一个字符是:{{ myString | first }}</p>

{# 假设我们有一个数组或切片变量 #}
{% set myArray = ["苹果", "香蕉", "橘子", "梨"] %}
<p>数组的第一个元素是:{{ myArray | first }}</p>

{# 也可以直接应用于标签返回的数组 #}
{% tagList tags with limit="3" %}
<p>Tag 列表的第一个 Tag 名称是:{{ tags | first | attr:"Title" }}</p>
{% endtagList %}

Output example:

字符串的第一个字符是:A
数组的第一个元素是:苹果
Tag 列表的第一个 Tag 名称是:AnQiCMS

Note:If passedfirstThe filter's data is empty (nilPunctuation, empty string, or empty array, it will not return any content. Therefore, it can be combined with the tagifto determine if the data exists.


Second, usesliceFilter: Extract specified elements or characters by index

sliceThe filter provides more flexible slicing capabilities. Although it is mainly used to obtain a subsequence of a data set, it can also be ingeniously used to get the first element.When you need to get the first element included in the 'first N elements' as a new set, or when you need to precisely control the range in a specific scenario,slicea filter is very applicable.

Get the first element/character:

Byslice:":1"The form, you can extract the substring from the start position to the first element (excluding the second one).It should be noted that this method returns a new array or string (containing one element) rather than the first element itself.If you need to retrieve the first element of this new collection, you can combinefirstFurther processing of the filter.

Usage:

{# 假设我们有一个字符串变量 #}
{% set myString = "AnQiCMS 模板制作" %}
<p>截取字符串的第一个字符(返回一个包含单字符的字符串):'{{ myString | slice:":1" }}'</p>
<p>直接获取截取字符串的第一个字符:{{ (myString | slice:":1") | first }}</p>

{# 假设我们有一个数组或切片变量 #}
{% set myArray = ["前端", "后端", "设计", "运维"] %}
<p>截取数组的第一个元素(返回一个包含单元素的数组):{{ myArray | slice:":1" }}</p>
<p>直接获取截取数组的第一个元素:{{ (myArray | slice:":1") | first }}</p>

{# 获取前三个字符或元素 #}
<p>字符串的前三个字符是:'{{ myString | slice:":3" }}'</p>
<p>数组的前三个元素是:{{ myArray | slice:":3" | join:", " }}</p>

Output example:

截取字符串的第一个字符(返回一个包含单字符的字符串):'A'
直接获取截取字符串的第一个字符:A
截取数组的第一个元素(返回一个包含单元素的数组):[前端]
直接获取截取数组的第一个元素:前端
字符串的前三个字符是:'AnQ'
数组的前三个元素是:前端, 后端, 设计

sliceParameter format of the filter:

  • :Indicates starting from the beginning or ending at the end.
  • start:Indicates fromstart

Related articles

How to randomly select an element or character from an array or string in Anqi CMS template?

In Anqi CMS template, adding dynamism and fun to the content is an important aspect of improving user experience.Sometimes, we hope to display a random element on the page, such as a random slogan, a random recommendation tag, or randomly select a picture from a group of images.AnQi CMS relies on its flexible Django template engine syntax to provide a simple yet powerful way to meet this requirement, with the core tool being the `random` filter.

2025-11-08

The `count` filter calculates the number of times a keyword appears in an array, is it a partial match or a complete match?

In Anqi CMS template development, the `count` filter is undoubtedly a very practical tool for processing data, which can help us quickly count the frequency of a specific keyword in the data.However, when using this filter, many users may be puzzled: when it handles string and array data of different types, is the keyword matching method 'partial matching' or 'exact matching'?This is crucial for accurately presenting content data.

2025-11-08

How to calculate the number of times a keyword appears in a string or array in the Anqi CMS template?

In content operation, precisely mastering the use of keywords on the page plays a crucial role in SEO strategy, content quality assessment, and user experience optimization.AnQiCMS (AnQiCMS) is a flexible and efficient content management system with a powerful template engine built-in with many practical features, including a filter that can help us easily calculate the number of times keywords appear.Next, we will introduce how to use these features to count the frequency of keywords in the Anqi CMS template.### Deeply understand `count`

2025-11-08

Can the `contain` filter be used to check if a Map (key-value pair) contains a specific key?

AnQiCMS with its efficient and customizable features brings many conveniences to content management.In the daily operation of websites, we often need to dynamically adjust the page content in templates based on the existence of data.Among them, it is a common requirement to judge whether a specific key exists in the data of key-value pair (Map) type.Today, let's delve into whether and how the `contain` filter in AnQiCMS templates can be used to check for the existence of keys in a Map.

2025-11-08

How to get the last element/character of an array or string in Anqi CMS template?

In Anqi CMS template development, we often encounter the need to obtain the last element or character from a set of data (whether it is an array, list, or string).No matter whether it is to display the latest comments, the last item in the list, or extract the end information of specific text, understanding how to efficiently implement this at the template level will greatly enhance the flexibility and development efficiency of the template.The AnQi CMS template engine provides a simple and powerful filter function that can help us easily achieve this goal.

2025-11-08

How to perform addition operations on numbers in AnQi CMS template, including integers and floating-point numbers?

In AnQiCMS template design, sometimes we need to perform dynamic calculations on the numbers displayed on the page, such as calculating the total price of goods, displaying the cumulative points of users, or adjusting certain values.Whether it is a simple integer addition or involves decimal floating-point arithmetic, AnQiCMS provides intuitive and powerful methods to meet these needs.This article will delve into how to implement addition operations in the AnQiCMS template, making your content display more dynamic and practical.

2025-11-08

How does the `add` filter handle string concatenation operations?

When developing templates with Anqi CMS, we often encounter scenarios where we need to combine text fragments or numbers for display.Whether it is to build dynamic page titles, concatenate URLs, or perform simple numerical calculations on the page, flexible data processing capabilities are crucial.The Anqi CMS template engine provides a very practical tool - the `add` filter, which can help us easily achieve string concatenation and numeric addition operations.What is the `add` filter?

2025-11-08

How to set a default display value for possibly empty variables (such as arrays or strings) in Anqi CMS templates?

During the process of building a website with AnqiCMS, we often encounter such situations: a variable needs to be displayed in the template, but it may be empty for various reasons, such as not setting a thumbnail, a list without content, or optional information not filled in.If these empty variables are not processed, the page may appear with ugly blank areas, error messages, or layout errors, which undoubtedly affects the user experience.

2025-11-08