As an experienced AnQi CMS website operations personnel, I know the core position of content in the website, as well as the importance of template making in the effective presentation of content. When we need to accurately obtain and display the ID and title of the current page in a single-page detail template, AnQiCMS provides an intuitive and efficient template tagpageDetailTo complete this task.
Get the current page ID and title in the AnQiCMS template.
In the AnQiCMS template system,pageDetailThe tag is a key tool used specifically for handling single-page detail data acquisition.It allows us to flexibly extract various information from the current or specified single-page, including its unique identifier ID and user-readable title.
Understand the AnQiCMS template andpageDetailTag
AnQiCMS template engine uses a tagging syntax similar to Django, separating content and logic, making it easy for website maintenance and style customization. In the template file, variables are enclosed in double curly braces{{变量}}Definition, while logical control labels (such as conditional judgment, loop) use single curly brackets and percent signs{% 标签 %}.
pageDetailThis is a powerful logical label, mainly used in the context of single-page detail pages, or by explicitly specifying an ID to obtain detailed data for a single page. When you are in a single-page detail template (such aspage/detail.htmlorpage.htmlUsed inpageDetailWhen the tag is used, it intelligently identifies the single page being accessed and extracts the required data without needing to specify an ID.
Get the ID of the current single page
To get the current page ID, we can directly use it in the single page detail templatepageDetailthe tag and use it.namethe parameter toId. BecausepageDetailThe label has the ability to automatically identify the current page in a single-page detail page, so we usually do not need to provideidParameter.
For example, in yourpage/detail.htmlIn the template file, you can get the ID of the current page like this:
<p>当前单页面ID:{% pageDetail with name="Id" %}</p>
This code will directly output the numeric ID of the current page.
Display the title of the current page
Similar to obtaining an ID, to display the title of the current single page, we also usepageDetailtags, but this time we willnamethe parameter toTitle. This ensures that we are getting the formal title configured for the single page in the background.
In the same single page detail template, the code to display the title will be:
<h1>{% pageDetail with name="Title" %}</h1>
This will display the title of the current single page in the form of an H1 tag.
Practice Operation: Implement in the Template
In AnQiCMS, the detail template of a single page is usually located in the directory of the template theme you are currently using.page/detail.htmlorpage.htmlFile. To clearly demonstrate how to obtain the ID and title in these templates, we can combine them.
Here is a specific template code example that displays its ID and title on a single page:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>{% pageDetail with name="Title" %} - {% system with name="SiteName" %}</title>
</head>
<body>
<header>
<nav>
{# 导航栏内容 #}
</nav>
</header>
<main>
<section>
{# 使用pageDetail标签直接输出当前单页面的ID和标题 #}
<h1>当前页面标题:{% pageDetail with name="Title" %}</h1>
<p>当前页面ID:{% pageDetail with name="Id" %}</p>
{# 也可以将ID和标题存储到变量中,方便在模板其他位置复用 #}
{% pageDetail currentPageId with name="Id" %}
{% pageDetail currentPageTitle with name="Title" %}
<div class="page-info">
页面ID(来自变量):{{ currentPageId }}
<br>
页面标题(来自变量):{{ currentPageTitle }}
</div>
<article>
<h2>页面内容</h2>
{# 显示单页面的内容,需要使用safe过滤器以避免HTML转义 #}
<div>{% pageDetail with name="Content" %}{{ pageDetail.Content|safe }}</div>
</article>
</section>
</main>
<footer>
{# 页脚内容 #}
</footer>
</body>
</html>
In this example, we first in<title>within the tag usingpageDetailto get the title and then through{% system with name="SiteName" %}Retrieve the website name as the suffix of the title. Then, in the main body of the page, we use{% pageDetail with name="Title" %}and{% pageDetail with name="Id" %}Directly output the title and ID. To demonstrate variable reuse, we also show how to assign them tocurrentPageIdandcurrentPageTitleVariables are used later. Finally, it also shows how to display the content of a single page.
Extended Application: Retrieve information about other single-page applications.
pageDetailThe power of tags is not limited to ID and Title. By changingnamethe parameters, you can also obtain other rich information on a single page, such as:
- Single page link:
{% pageDetail with name="Link" %} - Single page description:
{% pageDetail with name="Description" %} - Single page content:
{% pageDetail with name="Content" %}(Note, often need to be accompanied by|safefilter to render HTML correctly) - Single-page thumbnail:
{% pageDetail with name="Thumb" %} - Single-page slide group image:
{% pageDetail pageImages with name="Images" %}{% for img in pageImages %}<img src="{{ img }}" />{% endfor %}
If you need to get information that is not the current single-page information, you can go throughidortokenThe parameter explicitly specifies the target single page. For example, to get the title of the "About Us" page with ID 5, even if you are not currently on that page, you can use{% pageDetail with name="Title" id="5" %}.
By combining these flexible tags and parameters, AnQiCMS makes template creation more efficient and customizable, ensuring that the website content is presented in the exact way you expect.
Frequently Asked Questions (FAQ)
Ask: How to get the ID and title of a specific single-page template (such as the homepage or list page)?
Answer:pageDetailTags support throughidortokenParameters to specify the single page to query. For example, if you want to display the title of the "Contact Us" single page with ID 8 on the home page of the website, you can use{% pageDetail with name="Title" id="8" %}. If you know its URL alias (for exampleabout-us), you can also use{% pageDetail with name="Title" token="about-us" %}to get the title.
Question:pageDetailWhat fields can be retrieved for a single page besides ID and Title?
Answer:pageDetailThe tag is very flexible, based onnameThe parameters differ, except for the ID (Id) and the title (Title),you can also get the link to a single page (Link), description (Description),complete content (Content),thumbnail large image (Logo),and thumbnail (Thumb) as well as a slide set diagram(Images) and other rich information. When obtaining such fields asContentwhich may contain HTML tags|safeit is usually recommended to use a filter to ensure that the content is rendered correctly.
Ask: If I use a single-page detail template andpageDetailthe current URL is not a valid single-page detail page, what will happen?
Answer: IfpageDetailThe tag is called in a non-single page detail page and does not passidortokenThe parameter explicitly specifies the page, it may not be able to obtain valid single page context information. In this case,pageDetailThe field attempted to be retrieved (such as ID or Title) may output as a blank value. To avoid this, make sure to call the template inpageDetailAt the moment, either the current URL indeed points to a single-page detail, oridortokenthe parameter provides a clear single-page identifier.