How will the content be directly output when the `variableName` of the `pageDetail` tag is not set?

Unlock the secrets of the AnQi CMS single-page content direct output:pageDetailThe concise power of tags

As an expert who has been deeply involved in website operation for many years, I am well aware that efficiently 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, whose concise and efficient template engine is very appealing to me.It provides great convenience in content display, especially for single-page content (such as "About Us

Today, let's delve into a seemingly simple but clever template tag in AnQi CMS -pageDetailWhen we are notpageDetailSet tags变量名称What kind of content is directly output, and what advantages this method can bring in actual operation.

pageDetailThe foundation of tags and direct output mechanism

In the template design of AnQi CMS,pageDetailLabels are specifically used to retrieve and display detailed information of a single page. It allows us to specifynameparameters to accurately extract specific fields of a page, such as the page titleTitle)、page content(Content)、page description(Description)etc.

Its cleverness lies in its flexible output mechanism.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 no variable name is set, the result is output directly.”}]pageDetailwhen labeling:

<div>单页标题:{% pageDetail with name="Title" %}</div>
<div>单页描述:{% pageDetail with name="Description" %}</div>

The template engine will query the current page title immediately when parsing to{% pageDetail with name="Title" %}(idortoken指定的页面标题),并将其值直接插入到该标签所在的位置,形成一段完整的HTML内容。Similarly, the page description will be output directly 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.

Content directly output application scenarios

This direct output feature is particularly practical in many common website operation scenarios:

  1. Page Title (Title) and meta description (Description): In<head>page of regional settings<title>Tags and<meta name="description">Tags, we usually only 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 from the background database and presented.

  2. Main content of the page (Content)For the core content area of a single page, such as the long text introduction on the "About Us" page, outputting directly is the most efficient way.

    <article class="main-content">
        {% pageDetail pageContent with name="Content" %}
        {{ pageContent|safe }} {# 注意:如果内容包含HTML标签,需要使用|safe过滤器防止被转义 #}
    </article>
    

    Here, I have written an extra step for safety, assigning topageContentVariables. But if you want to write 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>
    

    AnQiCMSpageDetailLabels when directly outputContentField, if the content 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, it is necessary to cooperate with|safeFilter usage. If the content is in Markdown format, you can also addrender=trueparameters, so that the system converts it to HTML before outputting, and then through|safeEnsure rendering.

  3. image links (LogoorThumb)When it is necessary to display a single-page thumbnail or Banner image, it is also a convenient practice to directly output the image URL.srcThe property is also a very convenient practice.

    <img src="{% pageDetail with name="Logo" %}" alt="{% pageDetail with name="Title" %}" class="page-header-image">
    

These scenarios have the common characteristic that our content processing is limited to "get 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 have a variable name? (Comparative thinking)

Of course, not all situations are suitable for direct output. When we indeed need to process the single-page content we obtain further,pageDetailIt is very necessary to specify a variable name for the label. For example:

  • Data processingIf 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 single-page field 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 be clearer and more efficient.

In summary, the security CMS ofpageDetailLabels provide a flexible way to display content.For simple, one-time content display needs, outputting without setting variable names can make your template code more concise and efficient.When it is necessary to perform deep processing of content or multiple references, using it in conjunction with variable names can provide greater control.Mastering these two usages will enable you to swim like a fish in the content operation of the AnQi CMS.


Common Questions and Answers (FAQ)

1. Why does AnQi CMS allowpageDetailnot to set variable names for tags? What are the benefits of doing so?

AnQi CMS allowspageDetailThe label does not set variable names, mainly to simplify template code and improve development efficiency.When you only need to retrieve and directly output a single field (such as title, description, image link) to a specific position in HTML, there is no need to introduce an additional variable to store the data.This approach makes the code more concise and clear, especially suitable for simple display scenarios where no secondary processing of data is required and data is used 'on-the-fly'.

2. Can you directly modifypageDetailThe content output uses a filter (Filters)?

Sure. WhenpageDetailWhen you output the label directly, you can immediately apply the filter after it. For example, if you want to output the single page content directly and ensure that the 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:15.But please note, for some complex operations, such as when you need to make conditional judgments based on output values, or when you need to refer to the same processed value multiple times, it is clearer to assign it to a variable first and then perform the operation.

3.pageDetailDirect output:ContentField, how will it be displayed if it contains HTML tags?

By default, to prevent security issues such as cross-site scripting (XSS), the template engine of Anqi CMS willpageDetailDirect output:ContentHTML tags in the field.Automatic escaping.This means that HTML tags will be displayed as plain text, rather than being parsed by the browser as actual HTML elements.pageDetailLabel after using|safeFilter, for example:{% pageDetail with name="Content" render=true|safe %}If the content is in Markdown format, remember to addrender=trueparameter, make sure it is rendered into HTML first and then through|safeOutput.