Security CMStdkLabel: Did you really use it correctly without setting the variable name?

As an experienced website operator, we know that every detail of a website can affect user experience and SEO optimization effects.In such an efficient and powerful content management system as AnQiCMS, the flexible use of template tags is the key to improving operational efficiency.Today, let's delve deeply into a common question in template development, which is also an Anqi CMStdkThe uniqueness of the label: when you calltdkWhat impact will it have if you choose not to set the variable name when labeling? What design philosophy and practical value does this entail?

tdkThe foundational role of tags

First, let's briefly review.tdkThe placement of tags in Anqi CMS. TDK, which stands for Title (title), Description (description), and Keywords (keywords), is the core information displayed on the website page in search engine results and is also the basis for SEO optimization.AnQi CMS knows its importance and therefore provides powerfultdkA tag that can intelligently retrieve and output the corresponding TDK information based on the settings of the current page (home page, article detail page, category list page, etc.), even including the canonical link (CanonicalUrl).

Its standard usage is usually like this:{% tdk 变量名称 with name="字段名称" %}. Here,nameThe parameter is used to specify what you want to getTitle/Keywords/DescriptionOrCanonicalUrl.

Without setting the variable name: the power of direct output

Then, when we omit the变量名称This part is, for example, written directly{% tdk with name="Title" %}How will AnQi CMS handle it?

The answer is actually very simple, but very practical:IftdkThe tag does not have a variable name set, its output result will be rendered directly at the position of the tag in the template.

Imagine that you usually would be in the HTML's<head>area to set the page<title>Label. One of the most common and direct uses is:

<head>
    <title>{% tdk with name="Title" %}</title>
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
</head>

In this example,{% tdk with name="Title" %}There is not one likeseoTitleSuch a variable to receive its value. It is like a plug-and-play output point, directly filling in the obtained page title text to<title>Tags inside. Similarly, the Keywords and Description content are presented in this direct manner.<meta>label'scontentthe attribute.

This design philosophy simplifies the writing of templates, especially suitable for those scenarios where you only need to directly obtain the TDK value and output it immediately.It reduces the definition of intermediate variables, making the code more concise and clear, easy to read and maintain.For the core TDK area of a website, this direct output method is undoubtedly the most common and efficient choice.

When should variable names be set?

Although direct output is very convenient, but in some cases, we still need to specify a variable name for the tag. This usually happens in the following situations:tdkThis usually happens in the following situations:

  1. When further processing of the TDK content is required:If you get the TDK value and want to perform some formatting, truncation, or other filtering operations, you must first assign it to a variable. For example, you may want to convert all keywords to lowercase or limit the length of the description:

    {% tdk seoKeywords with name="Keywords" %}
    <meta name="keywords" content="{{ seoKeywords|lower }}"> {# 使用 lower 过滤器将关键词转为小写 #}
    
    {% tdk seoDescription with name="Description" %}
    <meta name="description" content="{{ seoDescription|truncatechars:150 }}"> {# 截取描述为150个字符 #}
    
  2. When a conditional judgment needs to be made:Sometimes, we may want to decide whether to render a certain HTML element based on the existence or specific value of the TDK content. For example, the canonical link (CanonicalUrl) may not be present on every page, and we need to first determine its existence:

    {% tdk canonicalUrl with name="CanonicalUrl" %}
    {% if canonicalUrl %}
        <link rel="canonical" href="{{ canonicalUrl }}" />
    {% endif %}
    

    If not setCanonicalUrlAssign tocanonicalUrlWe will not be able toifConditional judgment.

  3. Use the same TDK value multiple times in the template:Although TDK information is generally output only once, in some special layouts, if you need to refer to the same TDK value multiple times in different locations of the template, assigning it to a variable can avoid repeated calls to the tag, improve efficiency, and maintain code cleanliness.

The balance of practice and practice

Of Security CMStdkThis flexibility in variable naming tags reflects the clever balance between usability and scalability of the system.

For most simple TDK output scenarios, such as in<head>directly defining the page's<title>and<meta>tags, omitting variable names directly is**practical. It makes template code more intuitive and concise.

When your content operation strategy requires more complex logical processing of TDK information (such as custom formatting, conditional display, etc.), it is fortdkLabeling a variable with a clear name can provide you with great flexibility and control.

In short, understandingtdkThe behavior of tags when not setting variable names can help us better utilize the template function of Anqi CMS and write high-quality templates that are efficient and meet business requirements.This not only improved the website's SEO performance, but also optimized the development and maintenance experience of the template.


Frequently Asked Questions (FAQ)

1. About AnQi CMS.tdkLabeling and using directly{{archive.Title}}or{{category.Description}}What is the difference?

tdkThe tag is a '万能' tag, it will intelligently obtain and output the most appropriate TDK information according to the type of the current page (home page, article, category, single page, tag page, etc.) and the TDK settings in the background. For example, on the article detail page,tdk with name="Title"Priority is given to obtaining the SEO title of the article, and if it is not set, it may fall back to the article title.{{archive.Title}}Then directly output the title field of the current article, without involving the priority judgment of TDK logic. In short,tdkThe tag focuses more on providing comprehensive SEO meta information and has an intelligent fallback mechanism; while{{archive.Title}}others directly obtain the specific fields of the content object.

2. If I don't want to set a variable name, but also want to filtertdkthe output of the tag (such as|safeor|truncatechars), can I do that?

Not allowed. In Anqi CMS template syntax, filters are applied to variables. If you do not specifytdkLabeling the output with a variable name means that its result will be output directly, without a 'variable' handle available for filter operations. Therefore, if you need totdkThe content obtained by the label must be assigned to a variable first before any filtering or processing.

3. This 'output without setting variable name' feature, does it also apply to other template tags of Anqi CMS?

Yes, many single-value retrieval tags of AnQi CMS follow this design philosophy. For example,systemtags are used to retrieve system configuration information,contacttags are used to retrieve contact information. When you call{% system with name="SiteName" %}or{% contact with name="Cellphone" %}When a variable name is omitted, the content of the variables will also be output directly to the template.When a more complex operation needs to be performed, it is also recommended to assign it to a variable.Consult the documentation of the specific tag, if its usage method mentions that "the variable name is not required, and if no variable name is set, the result is output directly", then it means that this tag has this feature.