The AnQiCMS is an efficient and flexible content management system that provides rich template tags and filters to help users achieve diverse content display. Among them,wordwrapFilter is a practical tool for automatic line breaks in long text. Dynamically adjusting its line break parameters is particularly important for improving the reading experience of website content, especially in responsive design.

UnderstandingwordwrapFilter

In the template design of AnQi CMS,wordwrapThe filter can automatically wrap a long text content according to the character length you specify.Its main function is to optimize the visual presentation of text and avoid the impact of excessively long single-line text on reading continuity.

wordwrapThe basic usage of the filter is very intuitive, you can apply it to any variable that contains text content. For example:

{{ article.Content | wordwrap:20 }}

This willarticle.ContentThe text content is attempted to be wrapped at every 20 characters.

It is worth noting that,wordwrapThe filter determines word boundaries mainly based on spaces when performing line breaks.This means that it will not break lines in the middle of words if it encounters continuous Chinese text or long words without spaces.This is an important feature that needs to be considered when in use, especially for Chinese websites.wordwrapThe filter operates by default on plain text, if your content contains HTML tags, using it directly may destroy the tag structure, causing the page to display abnormally.Therefore, it is usually necessary to use cautiously or consider additional factors before handling content that includes HTML.

Method to dynamically adjust the line break parameters.

To achievewordwrapFilter line parameter dynamic adjustment, we can introduce variable numbers in the template in many ways instead of writing fixed numbers.This greatly increases the flexibility of the template and the adaptability of the content.

1. Passing parameters through template internal variables.

The most direct way is to define a variable within the template file to store the line break length, and then pass this variable towordwrapFilter. This method is suitable for scenarios where line break parameters need to be unified in specific templates or blocks.

You can use{% set %}To define a local variable using a label:

{% set wrap_length = 25 %}
<p>{{ article.Description | wordwrap:wrap_length }}</p>

In this case, only the modification is neededwrap_lengthThe value, can globally adjust all templates using this variablewordwrapNewline parameter of the filter.

2. Pass parameters using system configuration or content fields

The more advanced dynamic adjustment method is to obtain the line break parameters from the backend management system of the Safe CMS.This way, website administrators can flexibly configure line break parameters through the backend interface without modifying the template code.

  • Get from the system global settings:You can add custom parameters in the "Global Function Settings" or "Content Settings" of the Anqi CMS backend. For example, add a parameter namedGlobalWrapLengthThe parameter, and set its value to30. Then, you can get this value in the template throughsystemtag:

    {% system global_wrap_length with name="GlobalWrapLength" %}
    <p>{{ article.Content | wordwrap:global_wrap_length }}</p>
    

    In this way, the whole website'swordwrapdefault parameters can be managed uniformly in the background.

  • From document or category custom fields:If different content types (such as articles, products) or different categories require their own unique line breaks settings, you can add custom fields to the content model or category. For example, add a field namedCustomWrapLengthThe numeric type field. When editing specific articles, fill in the line break length required for the article. You can call it like this in the article detail page template:

    {% archiveDetail article_wrap_length with name="CustomWrapLength" %}
    {% if article_wrap_length > 0 %}
        <p>{{ article.Content | wordwrap:article_wrap_length }}</p>
    {% else %}
        {# 如果自定义字段未设置,则使用一个默认值或全局设置 #}
        {% system default_wrap_length with name="GlobalWrapLength" %}
        <p>{{ article.Content | wordwrap:default_wrap_length|default:20 }}</p>
    {% endif %}
    

    This method provides extremely high flexibility, allowing different line break behaviors to be defined for each article, each product, and even each category.

3. Implement dynamic line breaks with conditional judgment

In some cases, you may want to apply different line break parameters based on different conditions.For example, use a shorter line break length on mobile devices to accommodate small screens, and a longer length on desktop devices.if标签) 来实现:

{% set current_wrap_length = 20 %} {# 默认值 #}

{# 假设您有一个变量 `is_mobile` 来判断当前设备类型 #}
{% if is_mobile %}
    {% set current_wrap_length = 15 %}
{% else %}
    {% set current_wrap_length = 30 %}
{% endif %}

<div class="content-block">
    {{ article.Content | wordwrap:current_wrap_length }}
</div>

In this way,wordwrap过滤器能够根据运行时环境或特定逻辑智能地调整其行为。

Precautions

  • 中文文本处理:[en]Again, I emphasize,wordwrapFilter is mainly identified by spaces to recognize line breaks.For continuous Chinese text, it will not be broken in the middle of the characters even if it exceeds the specified length.If you need to truncate Chinese text accurately or wrap lines, you may need to look for other custom filters or JavaScript solutions.
  • HTML 内容的兼容性:If you wish towordwrapThe content to be processed may contain HTML tags, and direct use of this filter may result in incorrect truncation of the tags. It is recommended that you apply this filter to text containing HTML.wordwrapBefore, remove the HTML tags first, or only use this filter on the plain text section.
  • safeHow to use the filter:WhenwordwrapProcessing the text after needs to be rendered as HTML content when (for example, if the original content contains<b>or<em>Label, and you want these labels to still be effective after line breaks), please make sure to add it in the final output when it is displayed|safeFilter to prevent HTML entities from being escaped.

Summary

Anqi CMS'swordwrapThe filter combines template variables and backend configurations to achieve flexible dynamic adjustment of line break parameters.This not only helps us better control the layout of text and improve the readability of content, but also allows us to quickly respond to changes in design and content strategy without modifying the code.Take full advantage of these features, which will make your website more attractive and professional in content presentation.


Common Questions (FAQ)

1.wordwrapCan the filter recognize and correctly handle HTML tags in text?In most cases,wordwrapThe filter is based on plain text logic for line breaks, it does not recognize HTML tags. If you use text containing HTML tags inwordwrapFilter, may cause the tag structure to be unexpectedly truncated or damaged, thereby affecting the display of the page. It is recommended to use when dealing with content containing HTML.wordwrapBefore, consider removing HTML tags first, or ensure that this filter is only applied to the plain text content section.

2. Why did I setwordwrap:20However, why doesn't the continuous Chinese text wrap at the 20th character? wordwrapThe filter mainly identifies