In Anqi CMS template design, we sometimes need to generate some placeholder text to test the layout or function,loremLabels are the tools to complete this task. And after these placeholder texts are generated, if you need to further analyze their word count information, such as wanting to know a paragraph byloremHow many words are contained in the randomly generated text label, at this time it is necessary to usewordcountfilter.
This article will introduce how to cleverly combine in the AnQiCMS template,loremTags andwordcountFilter to display the number of words in random text.
Get to knowloremLabel: generate placeholder text.
loremThe label is a very practical auxiliary tool in the AnQiCMS template, which can quickly generate random Latin text (lorem ipsum) of a specified length, commonly used in page layout, content filling, and other scenarios to avoid poor design effects due to the lack of real content in the early stages of development.
loremThe basic usage of tags is relatively intuitive. It can accept several parameters to control the length and type of generated text:
- Quantity: Specify the number of words, paragraphs, or characters to be generated.
- Method: It is usually
w(words, words),p(paragraphs, paragraphs) orb(bytes, bytes/characters). - randomAn optional parameter that, if included, will make the generated text more random.
For example, if we want to generate 25 random words of text, we can write it like this:{% lorem 25 w %}
IntroductionwordcountFilter: Count the number of words
In the AnQiCMS template system,wordcountis a powerful filter, specifically designed to count the number of words in a string.It counts words by spaces as separators. This means that whether the text contains punctuation or not, as long as they are separated by spaces,wordcountIt will be considered as a separate word.
wordcountThe usage of the filter is also very simple, just pass the text content that needs to be counted through the pipe symbol|and pass it to it: {{ 文本内容 | wordcount }}
This filter returns an integer representing the total number of words counted.
Practice: GetloremGenerate the word count of the text.
Now, we willloremTags andwordcountThe filter is combined to achieve the goal of displaying a random number of text words.
due toloremThe label will output the text content directly, andwordcountThe filter needs to receive a variable or expression as input, therefore, we need to utilizefiltertags toloremcapture the output of the tag and pass it towordcountfilter.
The following is a specific template code example:
{# 生成一段包含25个单词的随机文本,并直接统计其单词数量 #}
<p>生成的随机文本中包含的单词数量为:
{% filter wordcount %}
{% lorem 25 w %}
{% endfilter %}
</p>
{# 如果需要同时显示生成的文本内容和其单词数量,可以先将文本内容存储在一个变量中 #}
{% set generatedText = "" %}
{% filter set generatedText %}
{% lorem 50 w %}
{% endfilter %}
<div style="border: 1px solid #eee; padding: 10px; margin-top: 15px;">
<p>以下是生成的随机文本:</p>
<p>{{ generatedText }}</p>
<p>这段文本的单词数量为:{{ generatedText | wordcount }}</p>
</div>
In this code, the first example uses directlyfilter wordcountenclosedloremtags, like thisloremThe generated text will be used aswordcountinput for counting and the result will be displayed directly.
The second example is more flexible. We first defined a namedgeneratedTextThen, throughfilter set generatedTextstructure, and thenloremassigned the text content generated by the tag to this variable. This way,generatedTextA variable contains actual random text, and we can reference it multiple times in the template, whether it is displayed directly or applied againwordcountA filter to display the number of words. This method is very useful when the generated text needs to be reused.
By such a combination, we can not only conveniently generate placeholder text, but also accurately obtain the word count, which provides great convenience in page layout adjustment, testing content length limits, and other aspects.
Summary
Display in AnQiCMS templateloremThe tag generates the number of words in the text, which needs to be used togetherloremThe tag generates text, and throughfilterThe tag passes the generated text content towordcountThe filter performs statistics. This method is concise and efficient, and it helps us better manage and test template content.
Frequently Asked Questions (FAQ)
1.wordcountHow does the filter define a 'word'? Will punctuation in the text affect the count?
wordcountThe filter mainly uses spaces to distinguish words. Generally, any character separated by spaces is considered a word.Punctuation marks (such as commas, periods, question marks, etc.), if they are not separated by spaces from adjacent words, are usuallywordcountIt is considered as part of a word and will not be counted as a separate word.For example, 'hello,world' is counted as one word, while 'hello, world' is counted as two words.
2. Can I also countloremthe number of characters in the generated text?Of course you can. AnQiCMS template provideslengtha filter that can be used to count the number of characters in a string (including spaces and punctuation). If you want to count the number of characters, you can{{ generatedText | wordcount }}Replace{{ generatedText | length }}.
3.loremCan the text generated by the label be used for actual website content?
loremThe label is mainly used to generate placeholders (placeholders) or test data.Its text is meaningless Latin, not suitable for direct use as actual website content unless your website target audience is Latin scholars or has special needs.Before the website goes live, please be sure to replace it with meaningful real content.