In the template design of AnQi CMS, we sometimes need to generate some placeholder text to test the layout or functionality,loremTags 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 how many words a passage byloremHow many words are included in the randomly generated text for tags, which is when you need to usewordcountthe filter.

This article will introduce in detail how to cleverly combineloremtags andwordcountFilter to display the number of random text words.

RecognizeloremLabel: Generate placeholder text

loremThe label is a very practical auxiliary tool in AnQiCMS templates, 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 a lack of real content in the early stages of development.

loremThe basic usage of the label is relatively intuitive. It can accept several parameters to control the length and type of the generated text:

  • Quantity: Specify the number of words, paragraphs, or characters to be generated.
  • Methodautowautopautobauto
  • autoAn optional parameter, if included, the generated text will be 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,wordcountIt is a powerful filter, specifically designed for counting the number of words in a string.It counts words by separating them with spaces.wordcountit will be treated as a separate word.

wordcountThe usage of the filter is also very simple, you just need to pass the text content to be counted through the pipe symbol|Pass it to it:{{ 文本内容 | wordcount }}

This filter returns an integer representing the total number of words counted.

Practice: Getloremthe word count of generated text.

Now, we willloremtags andwordcountThe filter combines to achieve the goal of displaying a random number of text words.

Due toloremThe tags will directly output the text content, whilewordcountThe filter needs to receive a variable or expression as input, so we need to usefiltertags toloremcapture the output of the tag and pass it towordcountFilter.

Here 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 wordcountwrappingloremtags, so thatloremthe generated text will be used aswordcountthe input for statistics and the result will be displayed directly.

The second example is more flexible. We first defined a variable namedgeneratedText. Then, through thefilter set generatedTextstructure, we assigned theloremtext content generated by the tag to this variable. This way,generatedTextThe variable includes the actual random text, and we can reference it multiple times in the template, whether it is displayed directly or applied againwordcountFilter to display the word count. This method is very useful when the generated text needs to be reused.

Through such combinations, we can not only conveniently generate placeholder text, but also accurately obtain the number of words, which provides great convenience in adjusting page layout, testing content length limits, and other aspects.

Summary

Display in AnQiCMS templateloremThe number of words generated by the tag, needs to be used in combinationloremTag to generate text, and pass throughfilterTag to pass the generated text content towordcountFilter statistics. This method is concise and efficient, and it helps us better manage and test template content.


Common Questions (FAQ)

1.wordcountHow does the filter define a 'word'? Does the presence of punctuation affect the count? wordcountThe filter distinguishes words mainly by spaces.In general, if characters are separated by spaces, they are considered to be a word.wordcountConsidered as part of a word, rather than as a single word.For example, "hello,world" is counted as a single word, while "hello, world" is counted as two words.

2. Can I also count the number of wordsloremin the generated text?Of course. 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,{{ generatedText | wordcount }}Replace with{{ generatedText | length }}.

3.loremCan the text generated by tags be used for actual website content? loremTags are mainly used to generate placeholders (placeholder) or test data.Its text is meaningless Latin and is 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 real content that has actual meaning.