How to retrieve and display the detailed content of the current article in AnQiCMS

In Anqi CMS, you need to retrieve and display the detailed content of the current article, mainly focusing on the organization of template files and built-in template tags.Understanding these core mechanisms allows you to flexibly control the presentation of articles on the front page.

Core Concept: The composition of the article detail page

In Anqi CMS, the detailed content of each article is usually displayed through a specific template file.This template file will be automatically or manually called by the system according to the content model (such as "article model" or "product model") it belongs to and the settings in the background.By default, AnQi CMS will search fortemplate/{你的模板目录}/{模型table}/detail.htmlThis path is used to render the article detail page. For example, if your article belongs to the "Article Model" (the defaulttableThe name might bearchive), then the system will try to loadtemplate/{你的模板目录}/archive/detail.htmlfile.

In this detail page template, we need to use the powerful template tags provided by Anqicms to "grab" the various data items of the article and display them according to the design.archiveDetailTags are the core to obtain detailed information of articles.

Deep understandingarchiveDetailTag

archiveDetailTags are specifically used to extract detailed data of a single article from the database. Its basic usage is{% archiveDetail 变量名称 with name="字段名称" %}.

  1. To obtain the current article dataWhen you are on the article detail page (such asarchive/detail.htmlUsed inarchiveDetailWhen tagging, it is usually not necessary to specify the article's ID. The system will intelligently identify the article being accessed and automatically retrieve its data.For example, to get the title of the current article, you just need to do this:{% archiveDetail with name="Title" %}

  2. BynameThe parameter specifies the field to be retrieved archiveDetailTag throughnameSpecify the specific field of the article you want to retrieve. These fields include the basic information of the article, SEO information, the content itself, and fields customized through the content model.

    Here are some commonly used fields and their applications:

    • Article title (Title):{% archiveDetail with name="Title" %}This will directly output the title of the article.

    • Article content (Content):{% archiveDetail with name="Content" %}This is the main part of the article page. It is important to note that if the article content contains HTML tags (such as images, links, formatted text, etc.), in order to ensure that these HTML codes can be parsed correctly by the browser rather than displayed as plain text, it is usually necessary to cooperate with|safeThe filter is used. If the article is written in Markdown and you have enabled the Markdown editor in the background, Anqi CMS will automatically convert it to HTML; if you need to manually control it, you can add it in the tag.render=trueorrender=falseParameters. Example:{% archiveDetail articleContent with name="Content" %}{{articleContent|safe}}{% endarchiveDetail %}

    • Publish time (CreatedTime), and Update time (UpdatedTime): These fields return timestamps, and in order to display them in a readable date format on the page, you need to usestampToDateTimestamp formatting tags. Example:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}(wherearchiveIt is througharchiveDetailTag-defined variable name,2006-01-02 15:04Is the Go language time formatting standard.

    • Article link (Link) Keywords (Keywords), Description (Description), Page views (Views): These fields are similar in terms of access methodTitle.{% archiveDetail with name="Link" %} {% archiveDetail with name="Keywords" %} {% archiveDetail with name="Description" %} {% archiveDetail with name="Views" %}

    • Cover image (Logo/Thumb/Images):LogoGenerally refers to the single main image or large image of an article,Thumbwhich is its thumbnail.ImagesIt may also be a group of images (multiple images). Example:<img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %}" />If there are multiple images, they need to be defined as variables and cycled:{% archiveDetail archiveImages with name="Images" %} {% for img in archiveImages %} <img src="{{img}}" alt="文章图片" /> {% endfor %} {% endarchiveDetail %}

    • Category (Category)This field can retrieve the detailed information of the article category it belongs to. If you need to display the category name or link, you can define it as a variable and then access its properties, or use it directly.categoryDetailTag acquisition. Example:{% archiveDetail currentCategory with name="Category" %} <a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a> {% endarchiveDetail %}

    • Tag tag (tagList): The Tag tag of the article is usually notarchiveDetaila direct attribute, but throughtagListtags to obtain. You can inarchiveDetailCall in the contexttagListAnd specifyitemIdFor the current article ID. Example:{% tagList tags with itemId=archive.Id %} {% for tag in tags %} <a href="{{ tag.Link }}">{{ tag.Title }}</a> {% endfor %} {% endtagList %}

    • Custom field (archiveParamsOr retrieve by name)These fields can be obtained in the template in two ways: by content model definition. It supports additional custom fields.

      • Directly by field name.If the name of the field to be called of the custom field isauthorthen it can be{% archiveDetail with name="author" %}.
      • loop through all custom fields: Use.archiveParamsLabel to iterate over all custom fields, which is very useful when the field names are uncertain or when a unified display is needed.{% archiveParams params %} {% for item in params %} <span>{{ item.Name }}:{{ item.Value }}</span> {% endfor %} {% endarchiveParams %}

Step by step to build the article detail page.

After understanding the core tags, we can follow the following steps to build a feature-complete article detail page:

  1. Confirm the location of the template fileFirst, you need to find or create your article detail page template file. For example, if it is an article model, it is usually intemplate/你的模板名称/archive/detail.html.

  2. Get and display basic article informationIndetail.htmlIn the file, you can first place the article title and main content.

    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="article-content">
        {% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}{% endarchiveDetail %}
    </div>
    
  3. Display auxiliary informationNext, add the publication date, views, category, and Tag tags, etc., as auxiliary information.

    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        <span>浏览量:{% archiveDetail with name="Views" %}</span>
        <span>所属分类:
            {% archiveDetail currentCategory with name="Category" %}<a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a>{% endarchiveDetail %}
        </span>
        <span>标签:
            {% tagList tags with itemId=archive.Id %}{% for tag in tags %}<a href="{{ tag.Link }}">{{ tag.Title }}</a>{% endfor %}{% endtagList %}
        </span>
    </div>
    

    (Note:archive.CreatedTimeHerearchiveIs a hypothetical variable name, if you have not passedwithThe parameter willarchiveDetailThe result is assigned toarchive