How to conditionally generate a URL based on the settings of `BaseUrl` or `MobileUrl` in the `tag-system` tag?

Calendar 👁️ 71

How to accurately reach: How to generate URLs based on conditions in AnQi CMS?BaseUrlorMobileUrlHow to generate URLs based on conditions?

In today's multi-screen interconnected era, providing users with a smooth and device-adaptive content experience is the key to the success of website operation.Whether on PC or mobile, we hope users can access the page most suitable for their devices.Anqi CMS, this is an enterprise-level content management system developed based on the Go language, which provides powerful tools for content operators to meet this challenge with its efficient and flexible features.Today, as an experienced website operations expert, I will take you to delve into the template tag system of Anqi CMS, especially how to use it cleverlysystemin the labelBaseUrlandMobileUrlSet, to conditionally generate URLs that adapt to different scenarios.

AnQi CMS multi-device support strategy

Let's briefly review the design philosophy of Anqi CMS in terms of multi-device support.The system provides various website modes, including "adaptive", "code adaptation", and "PC+mobile independent site" modes.

  • Adaptive mode: One code fits all screens, the URL remains consistent.
  • Code Adaptation Mode: Loads different styles or JS logic based on device type, the URL usually remains consistent.
  • PC+mobile independent site modeThis is the core scenario of this discussion, meaning that your PC site and mobile site may have completely independent domain names or subdomains (for example,www.yourdomain.comused for PC,m.yourdomain.comUsed for movement).In this mode, precise control of the link generation method in the template is particularly important.

The strength of AnQi CMS lies in its ability to expose these operational configurations to frontend developers through concise template tags, allowing website operators to easily implement complex logic control at the template level.

Understanding core tools:systemtags,BaseUrlandMobileUrl

In the template system of Anqi CMS,systemTags are the universal key to obtaining the global system configuration information. Through it, we can easily obtain the basic information such as the website name, Logo, filing number, and so on.And for today's topic - conditional URL generation,systemin the labelBaseUrlandMobileUrlThe attribute plays a core role.

  • BaseUrlThis property corresponds to the "Global Function Settings" configured in the Anqi CMS backend, which is usually the main domain name of your PC website.When your website does not have a separate mobile domain, it is also usually used as the base URL for all links.

    • Example of acquisition method:{% system with name="BaseUrl" %}
  • MobileUrlThis property corresponds to the 'Mobile End Address' in the 'Global Function Settings'. Only when your website adopts the 'PC+mobile independent site' model, and you have configured an independent domain for the mobile end,MobileUrlThere will be a non-empty value. It represents the base URL of your mobile site.

    • Example of acquisition method:{% system with name="MobileUrl" %}

Understanding the values of these two variables is the first step in implementing conditional URL generation.

Condition-based URL Generation: Principles and Practice

In actual template development, the most common requirement we encounter is: if an independent mobile end address is configured, then in some scenarios (such as providing a 'Visit Mobile Version' link in the navigation bar of the PC site, or in some specific templates where we want to link to the mobile version page), we should useMobileUrlOtherwise, useBaseUrlor keep the relative path

The template engine of AnQi CMS supports something similar to DjangoifLogical judgment label, this is exactly the tool we use to implement conditional URL generation. Its basic syntax is{% if 条件 %} ... {% else %} ... {% endif %}.

Now, let's demonstrate how to use this mechanism through a specific scenario:

Scene one: Generate switchable site links (from PC version to mobile version and vice versa)

Assuming you are developing a PC template and want to provide a link to the mobile version in the header or footer. This link will only be available if an independentMobileUrlIt will only be displayed and correctly point to the mobile site.

{# 首先,获取MobileUrl和BaseUrl的值,并赋给临时变量以便后续判断和使用 #}
{%- system mobileUrl with name="MobileUrl" %}
{%- system baseUrl with name="BaseUrl" %}

{# 判断是否存在独立的移动端地址 #}
{% if mobileUrl %}
    <div class="site-switcher">
        <a href="{{ mobileUrl }}" rel="nofollow">访问手机版</a>
    </div>
{% else %}
    {# 如果没有独立的移动端地址,可能采用自适应设计,或者不提供切换链接 #}
    <div class="site-switcher">
        <span>当前为{{ baseUrl }},已支持自适应浏览</span>
    </div>
{% endif %}

Similarly, if you are designing a mobile template and want to provide a link back to the PC, you can also use a similar logic:

{%- system mobileUrl with name="MobileUrl" %}
{%- system baseUrl with name="BaseUrl" %}

{% if baseUrl %}
    <div class="site-switcher">
        {# 假设当前在移动端模板,需要链接到PC版 #}
        <a href="{{ baseUrl }}" rel="nofollow">访问电脑版</a>
    </div>
{% endif %}

This code structure clearly expresses the logic of "if there is a mobile domain, display and link to it", greatly enhancing the robustness and flexibility of the template.

Scene two: Dynamically adjust the URL prefix of images and other resources

Although static resources such as images are usually throughTemplateUrlTag to get the basic path, but in some special cases, you may also want the image link to adjust according toBaseUrlorMobileUrlDynamically, for example, CDN configuration is different under different domain names.

{# 假设我们有一个图片路径是相对于根目录的,例如 "https://en.anqicms.com/uploads/image.jpg" #}
{%- system mobileUrl with name="MobileUrl" %}
{%- system baseUrl with name="BaseUrl" %}
{% set relativeImagePath = "https://en.anqicms.com/uploads/2023/example.webp" %}

<img src="{% if mobileUrl and currentDevice == 'mobile' %}{{ mobileUrl }}{% else %}{{ baseUrl }}{% endif %}{{ relativeImagePath }}" alt="动态图片" />

Please note the example mentioned abovecurrentDevice == 'mobile'is a hypothetical variable, native to AnqicmssystemThe label does not provide information about the current accessing device. Usually, device determination is completed at the HTTP request level by the server or Nginx, and then the corresponding PC or mobile template is loaded by AnQiCMS.This is to demonstrate more complex conditional logic. AtPC+mobile独立站点In the mobile template mode, you usually already use it in the mobile templateMobileUrland in the PC templateBaseUrlNo additional judgment is needed for the current device.

A more common scenario is, regardless of whether it is a PC or

Related articles

How to determine the relationship between the current time and the document publishing time in the AnQiCMS template, and display "New" or "Expired"?

## Content时效性:How to intelligently display “New Release” and “Expired” in AnQiCMS template In the ever-changing world of the Internet, the 'freshness' and 'timeliness' of content are key to attracting and retaining visitors on the website.As an experienced website operation expert, I am well aware of the powerful features of AnQiCMS (AnQi Content Management System), which not only provides flexible content management capabilities but also endows content with infinite display possibilities through its powerful template engine.Today, we will delve into how to use the AnQiCMS template

2025-11-06

How to conditionally use the `cycle` tag to alternate CSS classes within a `for` loop?

## How to conditionally use the `cycle` tag elegantly in the `for` loop of AnQiCMS?In website content display, especially on list pages, we often hope to enhance user experience and information readability through visual differences.For example, list rows alternate with different background colors, or certain types of list items require special styles to highlight.As an experienced website operations expert, I am well aware of the efficient and flexible content management system in AnQiCMS

2025-11-06

How to determine if the `Logo` field of `tagDetail` exists to display the cover image of the label?

As an experienced website operations expert, I know that how to flexibly and elegantly handle content display is the key to improving user experience and the professionalism of the website.Today, let's talk about how to cleverly judge whether the `Logo` field exists in the tag details of AnQiCMS (AnQiCMS), and decide whether to display the cover image of the tag.This is not just a technical implementation issue, but also about how we make the tabs more attractive.### Label cover image: The highlight of content operation In AnQi CMS

2025-11-06

How to determine if the document list under a specified tag is empty?

AnQi CMS, as an enterprise-level content management system built with Go language, has always been committed to providing users with efficient, customizable, and secure content operation solutions.In our daily content management work, especially when building dynamic, intelligent websites, we often need to flexibly adjust the display of the page based on the presence or absence of content.Today, let's delve deeply into a very practical and elegant feature of Anqi CMS - how to use the `tag-tagDataList` tag to determine if the document list under a specified tag is empty.In AnQi CMS

2025-11-06

How to use `if` judgment in AnQiCMS template to prevent rendering errors caused by undefined variables?

## Mastering AnQi CMS Template: Skillfully use `if` statements to say goodbye to undefined variable rendering errors!As a senior website operation expert, I am well aware of the importance of a stable and efficient website template for user experience and operation efficiency.AnQiCMS (AnQiCMS) offers powerful support for content management with its efficient architecture based on the Go language and a flexible Django-like template engine.However, even the most excellent system may cause rendering errors due to minor omissions in the template, the most common of which is accessing undefined variables. Today

2025-11-06

How to skip a specific `Id` page and not display it when looping through a single page `tag-pageList`?

## Masterfully Control Content Display: AnQi CMS `tag-pageList` Skipping Specific Pages As an experienced website operations expert, I am well aware of the importance of the flexibility of the content management system for operational efficiency and user experience.AnQiCMS (AnQiCMS) provides us with powerful content management capabilities with its high-performance architecture based on the Go language.

2025-11-06

In AnQiCMS template, how to determine if the date format converted by `stampToDate` is correct or valid?

In the vast realm of content management and website operation, every detail concerns the user experience and the accuracy of information transmission.The accurate display of date and time is an indispensable part of it.For operators and developers using AnQiCMS to build websites, how to elegantly handle timestamps in templates and ensure their correct and effective format is a topic worth in-depth discussion.Today, let's talk about the philosophy and practical wisdom of using the `stampToDate` tag in the AnQiCMS template.### Know

2025-11-06

How is Json-LD structured data automatically generated in AnQiCMS?

In today's highly competitive online environment, the SEO performance of a website is crucial, and structured data is one of the key technologies to enhance the visibility of a website in search results.As a system dedicated to providing an efficient and easy-to-use content management solution, AnQiCMS deeply understands the importance of structured data in improving website visibility.It not only provides users with convenient content publishing and management tools, but also quietly contributes to the SEO optimization of the website behind the scenes.

2025-11-06