Unlock the mystery of AnQi CMS single page content direct output:pageDetailThe power of concise tags
As an expert who has been deeply involved in website operation for many years, I know that effectively and flexibly displaying content is the key to success in daily content management.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, and its concise and efficient template engine is deeply to my liking.It provides great convenience in content display, especially for single-page content (such as "About Us", "Contact Information", etc.)
Today, let's delve into a seemingly simple but thought-provoking template tag in AnQi CMS -pageDetail. When we do notpageDetailLabel settings变量名称How content is directly outputted, and what advantages this method can bring in actual operation.
pageDetailThe basics of tags and direct output mechanism
In the template design of AnQi CMS,pageDetailTags are specifically used to retrieve and display detailed information about a single page. It allows us to specifynameparameters to accurately extract specific fields of a page, such as the page title (Title)、Page Content(Content)、Page Description(Description) and so on.
Its cleverness lies in its flexible output mechanism. The official documentation clearly states: 'Variable names are not required. After setting a variable name, subsequent calls can be made through the variable name, and if a variable name is not set, the result is output directly.'This means, when we use it like thispageDetailLabeling time:
<div>单页标题:{% pageDetail with name="Title" %}</div>
<div>单页描述:{% pageDetail with name="Description" %}</div>
The template engine parses to{% pageDetail with name="Title" %}and will immediately query the current page title (or throughidortokenThe specified page title) and insert its value directly into the position of the tag, forming a complete HTML content.Similarly, the page description will also be output in the same way.The entire process does not require us to assign the content to a temporary variable before outputting, which greatly simplifies the code.
the actual application scenario of content output directly
This direct output feature is particularly practical in many common website operation scenarios:
Page title (
Title) and meta descriptions (Description)In<head>the page of regional settings<title>Tags and<meta name="description">When labeling, we usually just need to directly output the values of these fields.<title>{% tdk with name="Title" %}</title> {# 使用tdk标签获取标题更通用,这里以pageDetail为例 #} <meta name="description" content="{% pageDetail with name="Description" %}">No additional variables are needed, the code is clear at a glance, directly obtained and presented from the background database.
The main content of the page (
Content): The most efficient way is to output directly for the core content area of a single page, such as the long text introduction on the "About Us" page.<article class="main-content"> {% pageDetail pageContent with name="Content" %} {{ pageContent|safe }} {# 注意:如果内容包含HTML标签,需要使用|safe过滤器防止被转义 #} </article>Here, I wrote an extra step for safety, assigning to
pageContentVariable. But if you want to write the code in the most concise way, you can directly write:<article class="main-content"> {% pageDetail with name="Content" render=true|safe %} {# render=true 确保Markdown内容被正确渲染,|safe 防止HTML标签被转义 #} </article>AnQiCMS'
pageDetailThe tag is output directlyContentWhen the field contains HTML,By default, it will be HTML-escaped for safetyTherefore, if you want the HTML content to be normally parsed and displayed by the browser, you must cooperate.|safeFilter usage. If the content is in Markdown format, you can also addrender=trueparameters to let the system convert it to HTML before outputting, and then pass|safeEnsure rendering.image links (
LogoorThumb)When it is necessary to display a single-page thumbnail or Banner image, the image URL can be output directly to thesrcattribute, which is also a convenient practice.<img src="{% pageDetail with name="Logo" %}" alt="{% pageDetail with name="Title" %}" class="page-header-image">
These scenarios have the common feature that the processing of the content is limited to 'retrieve and display', without the need for complex secondary processing, judgment, or repeated references in templates.The direct output mechanism greatly reduces the redundancy of template code, improves development efficiency and readability.
When is it necessary to name variables? (Comparative thinking)
Of course, not all situations are suitable for direct output. When we really need to process the content of a single page further, it ispageDetailIt is very necessary to specify a variable name with a tag. For example:
- Data processing againIf you need to truncate the title of a single page, perform case conversion, or make conditional judgments based on content length.
{% pageDetail pageTitle with name="Title" %} <p>处理后的标题:{{ pageTitle|truncatechars:10 }}</p> {% if pageTitle|length > 20 %} <p>标题较长,可能需要优化。</p> {% endif %} - Multiple referencesWhen the same field in a single page needs to be referenced multiple times in different positions of the template, and each reference may require different processing, assigning it to a variable will make it clearer and more efficient.
In summary, AnQi CMS'spageDetailTags provide a flexible way to display content. For simple, one-time content display needs, outputting directly without setting variable names can make your template code more concise and efficient.When dealing with content that requires deep processing or multiple references, using variable names in conjunction can provide greater control.Mastering these two methods will allow you to thrive in the content operation of Anqi CMS.
Frequently Asked Questions (FAQ)
1. Why does AnQi CMS allowpageDetailnot to set variable names for tags? What are the benefits of doing so?
AnQi CMS allowspageDetailTags not setting variable names is mainly to simplify template code and improve development efficiency.When you only need to retrieve and directly output a single page field (such as title, description, image link) to a specific position in HTML, you do not need to introduce an additional variable to store the data.This approach makes the code more concise and clear, especially suitable for those who do not process the data twice, only for simple "take and use" display scenarios.
2. Can you directly access when variable names are not set?pageDetailDo the output content use filters?
Sure. WhenpageDetailWhen outputting tags directly, you can immediately apply filters after them. For example, if you want to output a single page content directly and ensure that HTML is parsed, you can write it like this:{% pageDetail with name="Content" render=true|safe %}If you want to truncate a title that is output directly, you can do it like this:{% pageDetail with name="Title" %}|truncatechars:15Please note that for some complex operations, such as when you need to make conditional judgments based on output values or need to refer to the same processed value multiple times, it is clearer to assign it to a variable first before performing the operation.
3.pageDetailDirect outputContentWhat will happen to the field if it contains HTML tags?
By default, to prevent security issues such as cross-site scripting (XSS), the Anqi CMS template engine will processpageDetailDirect outputContentHTML tags in the field.Automatic escapingThis means that HTML tags will be displayed as plain text, rather than being parsed by the browser as actual HTML elements.If you are sure the content is safe and you want HTML to be parsed normally, be sure topageDetailAfter the label using|safefor example:{% pageDetail with name="Content" render=true|safe %}If the content is in Markdown format, remember to addrender=truethe parameter to ensure it is rendered into HTML before passing through|safeOutput.