In the template design of AnQi CMS, we often need to make the website content dynamic and random to avoid it being too static and to increase the browsing趣味性趣味性 of users. When you want to randomly select one from a series of preset options, text, or data to display, randomFilter is a very practical tool.It can help you easily randomly pick a character or element from a string or array (also known as a list or set), adding an unpredictable vitality to your website content.

randomFilter: Inject random vitality into your website content

randomThe core function of the filter is very intuitive: it randomly selects a value from the dataset you provide and outputs it. Whether you want to randomly select a character from a long string or randomly pick an element from a list containing multiple elements, randomFilters can complete this task in an extremely concise manner. It is particularly suitable for scenarios that require displaying diversity, personalization, or avoiding repetition of content.

Its uses are diverse, for example:

  • Display random proverbs or quotes:In the website's sidebar or footer, display a different famous quote or company philosophy every time the page is refreshed.
  • Content recommendation:Recommend a random one from similar articles, products, or services to increase exposure opportunities.
  • Personalized greeting:Display a random greeting message based on different times or user behaviors to enhance user experience.
  • Dynamic page elements:Randomly change a small icon, background color, or animation effect on the page to make it look more lively and vibrant.

How to userandomFilter

randomThe usage of the filter is very simple, following the filter syntax standard of AnQiCMS template engine:{{ 变量 | random }}.

1. Randomly select a character from a string:

When you are going torandomWhen a filter is applied to a string, it randomly selects one character from all the characters of the string to output.

{# 从字符串 "安企CMS真好用" 中随机抽取一个字符 #}
<p>今天随机抽取到的字是:{{ "安企CMS真好用"|random }}</p>

You may see any of the characters “安”, “企”, “C”, “M”, “S”, “真”, “好”, “用” when the page is loaded.

2. Randomly select an element from an array or list:

This might berandoma more common use case for filters. If you have an array or list containing multiple elements,randomThe filter can randomly select an element from it.

Assuming you have a set of predefined options, you can first convert them into an array and then perform random selection. In AnQiCMS templates, you can usesplitThe filter converts a comma-separated string into an array:

{# 将字符串“选项A,选项B,选项C,选项D”按逗号分割成数组,然后随机抽取一个 #}
{% set options = "选项A,选项B,选项C,选项D"|split:"," %}
<p>今天为您推荐的是:{{ options|random }}</p>

So, each time the page is refreshed, one of the options, 'Option A', 'Option B', 'Option C', 'Option D', will be displayed randomly.

If your variable itself is an array (for example, through other tags likearchiveListthe data obtained), you can use it directlyrandomFilter:

{# 假设 archives 变量是一个文档列表,随机抽取其中一篇文档的标题 #}
{% archiveList archives with type="list" limit="10" %}
    {% set random_archive = archives|random %}
    <p>随机推荐文章:<a href="{{ random_archive.Link }}">{{ random_archive.Title }}</a></p>
{% endarchiveList %}

Points to note

When usingrandomWhen using the filter, there are a few details to pay attention to in order to ensure it works as you expect:

  • Handling empty input:IfrandomThe input of the filter is an empty string or an empty array, it will not output any content. Therefore, in some cases, you may need to work withifTo check if a variable is empty or not, or to usedefaultSet a default value for the filter.
  • Non-collection type:If you willrandomThe filter is applied to a single non-collection value (such as a number), and it will directly return this value itself. For example,{{ 5|random }}The output result is5Instead of a random number within a certain range.randomThe filter is designed to extract froma setinstead of generating random numbers.
  • Dynamism: randomThe filter will be randomly selected each time the page is loaded.This means that every time the user visits the page or refreshes the page, they may see different random content, which is exactly the charm of its dynamic feel.

By using flexibilityrandomFilter, add more interactivity and fun to your AnQiCMS website, making content display more vivid and attracting users to continuously pay attention.


Common Questions and Answers (FAQ)

Q1:randomCan the filter directly generate a random number within a specified range?A1:randomThe filter itself cannot directly generate random numbers within a specified range.It is designed to randomly select one from an existing collection (such as the characters of a string, the elements of an array).If you need to generate a random number within a range, you usually need to process it in the backend logic and pass it to the template, or first create an array containing all possible numbers in the template and then randomly select from it.

Q2: If I want to randomly select multiple non-repeating elements from a list,randomcan the filter do that?A2:randomThe filter can only randomly select one element at a time. If you need to select multiple non-repeating elements,randomThe filter cannot be implemented directly.This usually requires more complex template logic, such as multiple extractions and recording of extracted elements within a loop, or passing a list of multiple random elements to the template after backend processing.

Q3: If my list or string contains duplicate elements,randomDoes the filter ignore duplicate items during extraction?A3: No.randomThe filter treats each element in the collection equally, even if they are duplicates. For example, if your list is["苹果", "香蕉", "苹果"]Then the probability of drawing 'apple' will be twice that of 'banana'. It simply selects one randomly from all available options without duplicates.