Secure CMStdkLabel: Have you really used it correctly if you don't set the variable name?

As a seasoned website operator, we fully understand that every detail of a website can affect user experience and search engine optimization (SEO) performance.In an efficient and powerful content management system like AnQiCMS, the flexible use of template tags is the key to improving operational efficiency.tdkThe uniqueness of a tag: When you call ittdkWhat effect will it have if you don't set a variable name when creating a tag? What design philosophy and practical value does it contain?

tdkThe foundational role of tags

First, let's briefly review.tdkThe placement of tags in the AnQi CMS.TDK, which stands for Title (Title), Description (Description), and Keywords (Keywords), is the core information displayed in search engine results for web pages and is the foundation for SEO optimization.tdkA tag that can intelligently obtain 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 standard 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.

Not setting the variable name: the power of direct output

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

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

Imagine, you usually would be in HTML's<head>area to set the page<title>Tag. A common and intuitive usage 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 no word likeseoTitleThis variable is used to receive its value. It is like a plug-and-play output point, directly filling in the obtained page title text.<title>Tag inside. Similarly, the content of Keywords and Description is presented in this direct manner<meta>Tagscontentin attributes.

This design concept 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 quickly and maintain.For the core TDK area of the website, this direct output method is undoubtedly the most common and efficient choice.

When should variable names be set?

Although outputting directly is very convenient, in some cases, we still need to assign a variable name to the label.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 on it, 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 might want to decide whether to render a certain HTML element based on the existence or a specific value of TDK content. For example, the canonical link (CanonicalUrl) may not be present on every page, and we need to first check its existence:

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

    If we do not setCanonicalUrlAssign to.canonicalUrlthe variable, we will not be able toifcondition 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 at different positions in the template, assigning it to a variable can avoid repeated calls to the tag, improve efficiency, and keep the code neat.

The trade-offs in practice and **practice**

Anqi CMS'stdkThis flexibility in naming variables with tags reflects the clever balance between usability and scalability of the system.

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

When your content operation strategy requires more complex logic processing of TDK information (such as custom formatting, conditional display, etc.), in order totdkLabel a variable with a clear name to provide you with great flexibility and control.

In short, understandingtdkThe behavior of the tag when no variable name is set can help us better utilize the template function of Anq CMS, and write high-quality templates that are both efficient and meet business needs.This not only improves the website's SEO performance, but also optimizes the development and maintenance experience of the template.


Common Questions (FAQ)

1. Secure CMS'stdkLabel and using directly{{archive.Title}}or{{category.Description}}What is the difference?

tdkA tag is a 'universal' tag that intelligently retrieves and outputs the most suitable TDK information based on the current page type (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"Get the SEO title of the article first, if it is not set, it may fallback to the article title.{{archive.Title}}Then directly output the current article's title field without involving the TDK priority judgment. In short,tdkLabels are more focused on providing comprehensive SEO meta information, and also have an intelligent fallback mechanism; and{{archive.Title}}While the others directly obtain specific fields of the content object.

2. If I don't want to set a variable name, but I want totdkapply a filter to the output of a|safeor|truncatecharstag (such as )?

Not allowed. In the template syntax of AnQi CMS, filters are applied to variables. If you do not specifytdkThe output of a label specifies a variable name, so its result will be output directly without a "variable" handle 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. Does this feature of 'outputting without setting variable names' also apply to other template tags in AnQi CMS?

Yes, many single-value retrieval tags in 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 are calling{% system with name="SiteName" %}or{% contact with name="Cellphone" %}When a variable name is omitted, their content is also directly output to the template.When performing more complex operations, it is also recommended to assign it to a variable.Check the documentation for the specific tag. If the usage method mentions 'The variable name is not required, and if no variable name is set, the result is output directly', it means that this tag has this feature.