In the template development of AnQiCMS,wordwrapThe filter is an important tool for optimizing text display.Its main function is to automatically wrap long lines of text at a set length, making the content more readable on the front-end page, and avoiding the appearance of horizontal scrollbars or text overflow layout issues.
Deep understandingwordwrapThe design intention of the filter
From its design, wordwrapThe filter is specifically designed to handle ordinary text content. It identifies words based on spaces in the text and tries to insert line breaks between words when they reach a specified length.For example, a long English paragraph, throughwordwrapProcessed, it can display lines with more standard length, enhancing the visual experience. However, it also has its characteristics: for continuous Chinese text, since Chinese words are usually not separated by spaces,wordwrapThe filter will not force a line break in the middle of Chinese words.
This means,wordwrapThe core logic is based on the concept of 'word' and 'space' in natural language, aiming to beautify and standardize the layout of plain text.
wordwrapThe essential difference with JSON data
Now, let's return to the core issue:wordwrapDoes the filter support formatting JSON data when outputting from AnQiCMS? The answer isnot supported.
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.{})and array(brackets)[])consists of,and uses colon:comma,The comma is used as a separator. JSON formatting usually refers to the 'beautification' of the original, compact JSON string, that is, adding appropriate indentation and line breaks to make the hierarchy clear and easy to read.
wordwrapThe filter does not understand the intrinsic structure of JSON data. If we pass a JSON string (even an unformatted compact string) towordwrapA filter that treats it as plain text.wordwrapThe space in JSON string may appear in places such as between keys and values (for example, there may be spaces between keys and values, or some spaces added manually for readability), but it cannot recognize JSON syntax elements such as object boundaries, array elements, or key-value separators. Worse still, ifwordwrapA newline character inserted in a critical position (such as in a key name or value) will directly destroy the validity of the JSON string, causing it to fail to parse.It also cannot achieve the hierarchical indentation required for JSON formatting.
In short,wordwrapThe filter does not have the ability to parse and understand JSON syntax, nor can it perform the indentation and structuring operations required for JSON beautification.It only manages the line length of ordinary text.
How to handle JSON data in AnQiCMS?
AlthoughwordwrapNot applicable to JSON, but in the application scenario of AnQiCMS, we may still encounter situations where we need to process JSON data, such as:
Display the original JSON data from the API:If your goal is to present the original JSON data (such as for debugging or displaying technical details) on a page and you want it to be readable, then the usual practice is to perform the pretty-print operation of the JSON data in the backend (the business logic layer of Go language). The Go language standard library contains
json.MarshalIndentThe function can conveniently implement this feature. The formatted JSON string is then passed to the template.In the template, due to the formatted JSON string, it may contain HTML special characters (such as<or>),and we hope that the browser will directly display the formatted text instead of parsing it, by placing it in<pre><code>tags and combiningsafefilters to avoid HTML escaping, for example:<pre><code>{{ formattedJsonString|safe }}</code></pre>Embed JSON-LD structured data:AnQiCMS itself supports JSON-LD structured data. As shown in the documentation, you can directly use it in the template.
{% jsonLd %}...{% endjsonLd %}Label to define and output JSON data that conforms to the JSON-LD specification. The system will automatically handle it on the page<head>Insertion of the part is correct. In this case, you provide the JSON structure, not thewordwraptext to be processed.Debugging Go struct data:If you only want to view the internal representation of the Go struct data passed to the template during development, rather than its JSON format, you can use
dumpOr filter.stringformat:"%#v"These filters will output the detailed information of the structure in the Go language, but please note that this is not a standard JSON format.
Summary
wordwrapThe filter is a practical tool in AnQiCMS for managing line length of plain text content.It is not suitable for JSON data formatting because its design philosophy and working mechanism are completely different from the requirements of JSON structure.For processing JSON data, we should use the JSON library of the backend language (such as Go) for formatting, or use the specific tags provided by AnQiCMS (such asjsonLdTo embed structured data. Understanding the use of each tool can greatly enhance website operation and development.
Frequently Asked Questions (FAQ)
1. I want to display a piece of raw JSON data from an external API in the template, and I hope it can automatically indent and format lineswordwrapIt can't be used, then what should I do?You should handle the JSON data returned by the API on the backend of AnQiCMS (usually the code logic layer in Go language). Before passing the data to the template, use Go language tojson.MarshalIndentThe functions format it into a string with indentation and line breaks.Then, pass this formatted string as a variable to the template.In the template, to ensure that the browser displays the JSON text correctly rather than attempting to parse the HTML special characters, it is usually placed in<pre><code>Tags inside, and use{{ yourFormattedJsonString|safe }}Output in this way.
2. Does the AnQiCMS template system have built-in filters likewordwrapThe same, directly pretty-print any data type (including JSON strings)?Based on the AnQiCMS document description,wordwrapThe filter is specifically designed for plain text, it cannot understand or format JSON structures. Although there isdumpFilters andstringformat:"%#v"Can be used to view the internal representation of Go structures, but they are not general 'pretty print' tools for JSON format.At present, AnQiCMS's template engine Pongo2 does not provide a built-in, general-purpose JSON pretty-print filter.This kind of requirement is usually recommended to be handled on the backend, or consider adding custom filters through secondary development.
3. If I just want to embed JSON-LD structured data in a template for SEO,wordwrapDoes the filter have an effect?
wordwrapThe filter has no effect on the JSON-LD structured data and should not be used with JSON-LD. AnQiCMS provides a special{% jsonLd %}...{% endjsonLd %}Label to handle such requirements. You just need to write JSON code that conforms to the JSON-LD specification within this tag.The system will automatically ensure that it is rendered correctly on the page, andwordwrapIt is completely unrelated.