Judging the existence and display of a single page in Anqi CMS template

The Auto CMS provides simple and efficient template tags, allowing content developers to easily make various dynamic content judgments and rendering in front-end templates.For the management and display of single pages (Page), the system has designed special tags to enable us to conveniently implement existence detection and content output for specific single pages.

Understand the single-page structure of AnQi CMS

Firstly, we need to understand the basic features of the single page in the Aanqi CMS.Under the 'Page Resources' module in the background, we can create and manage various single-page resources, such as company profile, service items, contact information, and so on.Each single page has a unique ID and can be set with a 'custom URL' (also known as Token).At the same time, we can also specify a 'single-page template' for single pages, which means that different single pages can have independent display styles.page/detail.htmlIt can also be for a specific ID or a custom URLpage/detail-{单页ID}.htmlorpage/{自定义URL}.html.

Determine if the single page exists

We can use the security CMS provided to judge if a single page exists in the template.pageDetailLabel. This label can be used to obtain detailed information of a single page, and its design also allows us to assign its result to a variable for subsequent logical judgment.

To implement existence checking, we can attempt to retrieve page data by using the single-page ID or its custom URL (Token) and store it in a temporary variable.If the variable is successfully assigned a page object, it means that the single page exists; conversely, if the variable is empty, it indicates that the single page does not exist.

For example, if we want to judge whether the single-page with ID5exists, and store the result inmySinglePagevariable, we can write the code like this:

{% pageDetail mySinglePage with id="5" %}

Or, if the custom URL of a single page is known:about-usThen you can use:

{% pageDetail mySinglePage with token="about-us" %}

After that, we can use:ifLogical judgment tags to check:mySinglePageWhether the variable contains valid data:

{% if mySinglePage %}
    {# 此处表示单页面存在,可以显示其内容 #}
{% else %}
    {# 此处表示单页面不存在,可以显示备用内容或不显示任何内容 #}
{% endif %}

Display the content of the single page

Once the page exists, we can access the properties of the single page and display its content through the variables previously assigned.mySinglePage.pageDetailThe label acquisition page object contains common fields such as title, content, link, description, etc.

The following are some common ways to access fields:

  • Page ID:{{ mySinglePage.Id }}
  • Page title:{{ mySinglePage.Title }}
  • Page link:{{ mySinglePage.Link }}
  • Page description:{{ mySinglePage.Description }}
  • Page content:{{ mySinglePage.Content|safe }}(Note)|safeFilter to prevent HTML content from being escaped
  • Page thumbnail large image:{{ mySinglePage.Logo }}
  • Page thumbnail:{{ mySinglePage.Thumb }}

For page contentContentField, if the background has enabled the Markdown editor, the content will be automatically converted to HTML. If manual control is required,pageDetailtag.render=trueorrender=falseParameters to force or not to perform Markdown conversion.

Comprehensive Example

Assume we want to display a "About Us

Backend configuration:

  • Create a page with ID5, with a custom URL ofabout-us, titled "About Us", and fill in the content.
  • Create a page with ID6, with a custom URL ofcontact-usTitle is 'Contact Us', and fill in the content with HTML or Markdown.

Template code example:

<div class="footer-links">
    <h3>快速链接</h3>
    <ul>
        {# 显示“关于我们”页面链接 #}
        {% pageDetail aboutPage with id="5" %}
        {% if aboutPage %}
            <li><a href="{{ aboutPage.Link }}">{{ aboutPage.Title }}</a></li>
        {% endif %}

        {# 判断“联系我们”页面是否存在并显示其内容 #}
        {% pageDetail contactPage with token="contact-us" %}
        {% if contactPage %}
            <li>
                <h4>{{ contactPage.Title }}</h4>
                <p>{{ contactPage.Description }}</p>
                {# 显示部分内容,注意使用|safe过滤器 #}
                <div class="contact-excerpt">{{ contactPage.Content|truncatechars_html:150|safe }}</div>
                <a href="{{ contactPage.Link }}">查看详情</a>
            </li>
        {% else %}
            <li>
                <h4>联系我们</h4>
                <p>电话: {% contact with name="Cellphone" %}</p>
                <p>邮箱: {% contact with name="Email" %}</p>
                <p>地址: {% contact with name="Address" %}</p>
            </li>
        {% endif %}
    </ul>
</div>

By means of the above method, we can flexibly determine the existence of a single page in the security CMS template and dynamically display its content as needed.This makes the website template more robust and intelligent, able to better adapt to changes in content management requirements.


Common Questions and Answers (FAQ)

1. Can I only determine if it exists by using a single page ID or a custom URL?

Yes, in the template tags of Anqi CMS,pageDetailTags are mainly used forid(Page ID) ortoken(Custom URL alias) to accurately find specific single pages. These parameters are the most direct and effective ways to identify and obtain individual pages.

2. How to ensure that the HTML tags in a single-page content are displayed correctly instead of being escaped into plain text?

When you output from the templatepageDetailtag to obtain the single-page content (such as{{ mySinglePage.Content }}When, to ensure that the HTML tags contained in the content can be parsed and rendered by the browser, you need to use|safefilter. For example:{{ mySinglePage.Content|safe }}。This is the default HTML escaping mechanism implemented by AnQi CMS for security considerations.|safeThe filter explicitly informs the system that this content is safe and does not require escaping.

3. How do I display all published single-page lists in a block and only show their titles and links?

You can usepageListLabel to get the list of all single pages. This label will return all pages as an array object, and you can iterate over this array to display the title and link of each page. For example:

<ul>
{% pageList allPages %}
    {% for page in allPages %}
    <li><a href="{{ page.Link }}">{{ page.Title }}</a></li>
    {% endfor %}
{% endpageList %}
</ul>

You can dynamically display all available single-page navigation or lists on the website in this way.