As an experienced website operations expert, I am well aware of the crucial role of an efficient content management system in the success of a website, especially in terms of flexible content display.AnQiCMS (AnQiCMS) with its powerful template functions and simple Go language architecture, has provided us with great convenience.Today, let's delve into how to easily retrieve the title, content, and links of a single page in AnQiCMS, so that you can better customize and display website information.
Get to know the 'single page' in AnQiCMS
In Anqi CMS, a single page usually refers to those pages with relatively independent content and fixed structure, which are not frequently updated, such as "About Us", "Contact Information", "Terms of Service", or "Privacy Policy" and so on.These pages often carry the basic information and brand story of a website, they play an important role in the website structure, but are different from continuously updated articles or product details.The 'Page Management' feature of AnQi CMS is born for this kind of content, it allows us to customize the content, SEO information, and even independent template files for each single page, such aspage/detail.htmlorpage/{单页面ID}.html.
Get the details of a single page:pageDetailThe clever use of tags
To get the details of a specific single page, AnQi CMS provides a very convenient template tag——pageDetail. This tag is like an intelligent guide, which can accurately extract the single page data you need from the database.
1. Accurately locate the target single page
UsepageDetailWhen labeling, you need to tell it which single page you want. You can do this by using the single page'sID(idparameters)or itsURL alias (tokenparameters)Specify precisely. For example, if you know that the ID of the "About Us" page is 1, you can write it like this: {% pageDetail with id="1" %}. If you are currently on a single page and want to get information about the current page, you do not even need to specifyidortoken,pageDetailThe tag will intelligently identify and obtain data from the current single page.
2. Get the title of a single page
Once a target single page is specified, it is easy to get its title. You just need topageDetailUsed in tagsname="Title"The parameter will directly output the clear title of the single page, used for the H1 tag or navigation link text.
{# 假设获取ID为1的单页面标题 #}
<h1>{% pageDetail with name="Title" id="1" %}</h1>
{# 如果是当前单页面的标题 #}
<title>{% pageDetail with name="Title" %}</title>
3. Extract the rich content of the single page.
The content is the core of a single-page. Usename="Content"Parameters, you can easily extract the main content of the page. This content is usually edited by the backend rich text editor, which may include images, formatted text, etc.
<div class="page-content">
{% pageDetail pageContent with name="Content" %}
{{ pageContent|safe }}
</div>
Here are some key points to note:
|safeFilter:If your single-page content contains HTML tags (which is very common in rich text editors), in order for the browser to correctly parse these tags and display the formatted effect rather than directly displaying the HTML code, youit mustUsed together|safeFilter. It tells the template engine that this part of the content is safe and does not need to be escaped.renderParameter (for Markdown):If your Anqi CMS has enabled the Markdown editor to write single-page content, and you want the template to automatically render Markdown syntax into HTML, you canname="Content"added afterwardsrender=true.
4. Get the access link of a single page
In addition to the title and content, the link (URL) of a single page is also indispensable. Usename="Link"You can obtain the access path of the single page parameter, you can apply it to the navigation menu, breadcrumbs, or jump buttons within the page.
<a href="{% pageDetail with name="Link" %}">了解更多</a>
5. Other useful information
pageDetailLabels can help you get the summary of a single page (Description),and thumbnail (Thumb) even large images (Logo) and various other information, all of which can be flexibly called throughnameparameters in the template.
Get single page list information:pageListFlexible use of tags
Sometimes, you may need to display multiple single-page lists in a certain area of the website (such as the footer navigation, sidebar), such as 'About Us', 'Terms of Service', and 'Contact Information', etc. At this time,pageListthe tag comes into play.
pageListThe label will return an array of single-page objects, you need toforiterate through these objects. In each iteration, you can access a single single-page object like this,item.Title/item.Link/item.DescriptionIn order to access the properties of each single page.
<nav class="footer-links">
<ul>
{% pageList pages %}
{% for item in pages %}
{# 判断是否是当前页面,可以添加active样式 #}
<li {% if item.IsCurrent %}class="active"{% endif %}>
<a href="{{ item.Link }}">{{ item.Title }}</a>
{# 如果需要显示简短描述,可以使用item.Description #}
{# <p>{{ item.Description }}</p> #}
</li>
{% endfor %}
{% endpageList %}
</ul>
</nav>
This code will iterate over all the created single pages and generate a list item with a title and link for each single page. You can adjust it as neededforContent in the loop, for example, add a thumbnailitem.ThumbOr a brief descriptionitem.Description.
Flexible use of template syntax
The Anqi CMS template system uses a concise syntax similar to Django, making the content call intuitive and efficient.
- Variable reference:Using double curly braces
{{变量名}}To output the value of a variable, for example{{ item.Title }}. - the tag to use:Use
{% 标签名 参数 %}Call functional tags, for example{% pageDetail %}and{% pageList %}. - Filter:Through the pipe
|Apply filters to process variables, for example|safeUsed to prevent HTML escaping.
Through proficiencypageDetailandpageListThese powerful template tags allow you to easily retrieve and display the title, content, and link of a single page in Anqi CMS, thereby providing users with a richer and more user-friendly content experience.
Frequently Asked Questions (FAQ)
Why is my single page content displaying HTML code instead of the parsed effect?This is usually because you did not use
|safeFilter. The Anqi CMS defaults to escaping all output content for safety.If the content contains HTML tags, be sure to use them in the template like this:{{ pageContent|safe }}.Can I in
pageDetailDo not specify an ID or URL alias in the tag, and directly get the single-page information of the current page?Absolutely, you can. When your template file is used to render a single page (for examplepage/detail.htmlor `page/{single