Make the Anqi CMS website content vivid: explorerandomFilter, the mystery of dynamic display realization
In this era of information explosion, an efficient and flexible website content management system is an indispensable tool for operators.The Anqiyun CMS, developed based on the Go programming language, is dedicated to providing a high-performance, easy-to-expand content management solution. It uses syntax similar to the Django template engine in template design, greatly simplifying the complexity of development and content presentation.But in addition to content publishing, we all hope that the website can maintain a sense of freshness, so that visitors can gain some new ideas every time they visit, and avoid monotonous static displays.How can you inject dynamic vitality into the website content without increasing additional development costs?Today, let's delve into a very practical and interesting filter in the AnQiCMS template---randomIt can help us easily achieve random display of content.
randomThe filter, as the name suggests, is the core function of randomly selecting and displaying an element from a given data set.This dataset can be a predefined list (or array) that you have set up, or it can be a plain string.Imagine that your website has a set of carefully prepared visitor tips, recommendations, product features, and even daily slogans for the website, and you want to see different content combinations every time a user visits, thenrandomThe filter is exactly for this.
applyrandomFilter, your website can immediately gain multiple advantages. First, it can significantly enhance the content ofdynamicityanduser engagementWhen visitors see different recommendations or tips each time they refresh the page, they feel that the website is more vibrant, thereby extending their stay time and increasing their interest in exploration.Secondly, for content operations, this is a kind ofan efficient and cost-effective carousel mechanismNo need for complex backend logic or database queries, just a simple configuration in the template layer can achieve random switching of elements such as marketing language, selected recommendations, 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.
then, this powerfulrandomHow does the filter work specifically? Its usage is very intuitive, usually in the form of{{ 您的变量 | random }} appearing in the AnQiCMS template.
Let's look at some actual examples:
1. Randomly select an element from the predefined array:
Assume your website needs to display a random quote or prompt in a certain area of the page. You can define an array containing multiple sentences directly in the template and then apply itrandomfilter.
{% set inspirationalQuotes = ["今天,你学习了吗?", "坚持不懈,方能成功!", "安企CMS让建站更简单。", "不要停止探索未知的世界。", "每次进步一点点,就是最大的成功。"] %}
<p><b>每日箴言:</b>{{ inspirationalQuotes|random }}</p>
In this code, we first usesetA tag defined a namedinspirationalQuotesarray. Then, throughinspirationalQuotes|randomEach time the page is loaded, the system will randomly select a sentence from this array to display to visitors. This way, your website can provide a fresh opening every time it is visited.
2. Select a random character from a string:
In most cases, we may prefer to select a complete text block from a list, butrandomThe filter also supports randomly selecting a character from a string. This may come in handy in certain趣味性 or captcha scenarios:
{% set brandName = "AnQiCMS" %}
<p>今天的幸运字符是:{{ brandName|random }}</p>
This code will randomly select a letter from the string "AnQiCMS" (e.g., 'A' or 'Q' etc.) for display.
3. Combine existing data to dynamically generate random content:
In actual operation, you may need to pass througharchiveListIn the document list obtained by the tag, randomly select one as a "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 utilizerandomto randomly select one from the filter.
For example, assuming you have already passedarchiveListGot a namelatestArticlesarticle 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 get the latest 10 articles, iflatestArticlesit exists, we directly pass the entire list torandomA filter is used to obtain a randomly selected article objectrandomArticleThen, we can display the title, link, and summary of the randomly selected article.
AnQiCMS a concise syntax similar to the Django template engine, making it easy to use and apply features includingrandomfilters and other functions are very easy to get started with and apply