Make the Anqi CMS website content vivid: ExplorerandomFilter, the mystery of dynamic display

random, it can help us easily achieve random display of content.

randomThe filter, as the name implies, its core function is to randomly select and display an element from a given data set.This dataset can be a list (or array) that you predefined, or it can be a regular string.randomThe filter is created for this purpose.

ApplicationrandomFilter, your website can immediately gain multiple advantages. First, it can significantly enhance the content ofdynamicityanduser engagement.When visitors see different recommendations or prompts each time they refresh the page, they will feel that the website is more lively, thereby extending their stay time and increasing their interest in exploration.efficient and cost-effective carousel mechanismNo complex backend logic or database queries are required. Simply configure the existing content at the template layer, and you can achieve random switching of elements such as marketing language,精选推荐, and call to action (CTA).This not only optimizes the user experience, but also provides a convenient way to test the attractiveness of different content without modifying the code.

So, this powerfulrandomHow to use the filter specifically? Its usage is very intuitive, usually appearing in the AnQiCMS template as{{ 您的变量 | random }}.

Let's look at a few actual examples:

1. Select an element randomly from a predefined array:

Assuming your website needs to display a random quote or tip in some area of the page. You can define an array containing multiple sentences directly in the template and apply itrandomFilter.

{% set inspirationalQuotes = ["今天,你学习了吗?", "坚持不懈,方能成功!", "安企CMS让建站更简单。", "不要停止探索未知的世界。", "每次进步一点点,就是最大的成功。"] %}
<p><b>每日箴言:</b>{{ inspirationalQuotes|random }}</p>

In this code, we first usesetA tag defines a variable namedinspirationalQuotesEnglish array. Then, throughinspirationalQuotes|randomIn each page load, the system will randomly pick a sentence from this array and display it to the visitor. This way, your website can provide a fresh opening every time it is visited.

2. From a string, randomly select a character:

Although in most cases, we may tend to select a complete text block from the list,randomFilter also supports randomly selecting a character from a string. This might come in handy in certain playful or captcha scenarios:

{% set brandName = "AnQiCMS" %}
<p>今天的幸运字符是:{{ brandName|random }}</p>

This code will randomly select a letter (such as 'A' or 'Q' etc.) from the string 'AnQiCMS' for display.

3. Dynamically generate random content based on existing data:

In actual operation, you may need to select from througharchiveListIn the document list obtained from the tags, randomly select one as "Special Recommendation" or "Lucky Article." AlthougharchiveListThe label itself does not have a direct random sort parameter, but you can first get the entire list and then utilizerandoma filter to randomly select one.

For example, assuming you have already gone througharchiveListObtained a list namedlatestArticlesarticle list:

{% archiveList latestArticles with type="list" limit="10" %}
    {# 此处省略文章列表的循环显示,我们关注如何随机选取 #}
{% endarchiveList %}

{% if latestArticles %}
    {% set randomArticle = latestArticles|random %}
    <div class="special-recommendation">
        <h3>今日特别推荐:</h3>
        <a href="{{ randomArticle.Link }}">{{ randomArticle.Title }}</a>
        <p>{{ randomArticle.Description|truncatechars:100 }}</p>
    </div>
{% endif %}

Here, we first obtained the latest 10 articles, iflatestArticlesexists, then the entire list is passed torandomFilter, thus obtaining a randomly selected article objectrandomArticle. Then, we can display the title, link, and introduction of this randomly selected article.

AnQiCMS such a concise syntax similar to Django template engine makes it very easy to get started and applyrandomfilters and various other functions are very easy to learn and use