As an experienced security CMS website operation personnel, I know that it is crucial to flexibly control the display of content in template development, especially for single-page content such as "About Us" and "Contact Us" that may require dynamic judgment and display.In actual operation, we often need to decide whether to render a block or link based on the existence of a page, in order to ensure the accuracy of website content and user experience.Below, I will elaborate on how to determine if a single page exists in the Anqi CMS template and display its content.

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

AnQi CMS provides simple and efficient template tags, allowing content developers to easily judge and render various dynamic content on the front-end template.For the management and display of single pages (Page), the system has designed special tags to enable us to conveniently implement the existence detection and content output of specific single pages.

Understand the single-page structure of AnQi CMS

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

Determine if the single page exists

We can use Anq CMS to determine whether a single page exists in the template,pageDetailThe tag can not only be used to obtain detailed information of a single page, but its design also allows its results to be assigned to a variable for subsequent logical judgment.

To implement existence judgment, we can try to retrieve page data through the ID of a single page 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;On the contrary, if the variable is empty, it means the single page does not exist.

For example, if we want to determine 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 knownabout-us, then you can use:

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

After that, we can useifLogical judgment tags to checkmySinglePageWhether the variable contains valid data:

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

Display the content of a single page

Once the single page exists, we can access the properties of the single page through the previously assignedmySinglePagevariable and display its content.pageDetailThe tag retrieves the page object that includes commonly used fields such as title, content, link, and description.

The following are some ways to access commonly used 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 full image:{{ mySinglePage.Logo }}
  • Page thumbnail:{{ mySinglePage.Thumb }}

For the page contentContentfield, if the background enables the Markdown editor, the content will be automatically converted to HTML. If manual control is needed, you can do so bypageDetailthe tag withrender=trueorrender=falseThe parameter forces Markdown conversion or not.

Comprehensive example

Assuming we want to display a "About Us" link in the website footer, and if the "Contact Us" page exists, then display its title and some content, otherwise display a default contact information.

Backend configuration:

  • Create an ID for5a single page with a custom URL asabout-us, titled “About Us”, and fill in the content.
  • Create an ID for6a single page with a custom URL ascontact-usContact us title, and fill in the content containing 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 using the above method, we can flexibly determine the existence of a single page in the Anqi CMS template and dynamically display its content as needed.This makes the website template more robust and intelligent, able to better adapt to the changes in content management requirements.


Frequently Asked Questions (FAQ)

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

Yes, in Anqi CMS template tags,pageDetailTags mainly throughid(Page ID) ortoken(Custom URL alias) to accurately find a specific single page. These parameters are the most direct and effective ways to identify and retrieve a single page.

2. How to ensure that HTML tags in the single page content are displayed correctly and not escaped into plain text?

When you output from the templatepageDetailcontent obtained from the tag (for example{{ mySinglePage.Content }}) To ensure that the HTML tags within the content are parsed and rendered by the browser, you need to use|safea filter. 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 can I display all published single page lists in a block and only show their titles and links?

You can usepageListTag to get the list of all single pages. This tag will return all pages as an array object, 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>

In this way, you can dynamically display all the available single-page navigation or list on the website.