How to specify the file path of the public code snippet included when using the `include` tag in the template?

Calendar 👁️ 61

As an experienced website operations expert, I am well aware of the importance of an efficient content management system (CMS) for website operations.AnQiCMS, with its flexibility and powerful template functions, provides a solid foundation for building various types of websites.Today, let's talk about one of the very practical functions that can greatly improve the efficiency of template development -includeTags, especially how they specify file paths when referencing public code snippets, is often a confusing place for beginners even experienced developers.

Why choose to useincludeTag?

Imagine the header (header), footer (footer), or sidebar (sidebar) of our website, they are almost on every page.If you copy and paste this code on every page, then if you need to change a tiny link or a word, we have to repeat the operation in dozens or even hundreds of files, which is a nightmare.includeThe appearance of tags is to solve this pain point. It allows us to extract these frequently repeated code snippets, save them as independent module files, and then simply reference them where needed.The benefits of doing so are obvious: the code is cleaner, the structure is clearer, it is also more efficient to maintain, and it can ensure consistent visual and functional experience between different pages of the website.

AnQiCMS Template File Organization Overview

All template files in Anqi CMS are stored in the same place./templateUnder the root directory. Each independent template theme, for example, we may have a theme nameddefaultThe default theme or one you customizedmy_themeEach topic will have its own independent folder, for example/template/default.includeThe file path referenced by the tag is based on the current theme directory. This means that when you use a theme withinincludeWhen a tag is used, the system will first look for the corresponding file in the theme folder.

To better organize public code snippets, Anqi CMS encourages them to be stored in the theme directory.partial/In the subdirectory. For example, in/template/default/partial/Under the directory, you might findheader.html/footer.html/sidebar.htmlwait for files. This convention not only makes the file structure clear at a glance, but also facilitates teamwork and project management. If you need to create independent templates for mobile devices, then inmobile/In the subdirectory, you will also see similarpartial/The internal file organization structure is consistent with the PC theme.

How to specify the path of the imported file?

Understood the file organization structure, specifying the path becomes simple. AtincludeIn the tag, you can directly write the path relative to the root directory of the current topic. For example, to include the header file mentioned earlier, no matter where you are/template/default/partial/header.htmlthe page header file, no matter where you are/template/default/index.htmlOr/template/default/archive/detail.htmlReference, the path should be written like this:

{% include "partial/header.html" %}

Here"partial/header.html"clearly indicates that the file is located in the current active theme directorypartialfolder. Even if the code snippet you want to reference is not inpartial/Under the directory, but rather in other custom folders within the theme, such as/template/default/blog/post-meta.htmlWhen you reference it, you only need to write the relative path starting from the root directory of the theme:

{% include "blog/post-meta.html" %}

The key is to always consider your reference path as starting from the root directory of the currently active theme (for example/template/default/)Start. The Anqi CMS template engine intelligently searches for these files within the framework of the current theme, avoiding confusion from complex absolute paths or cross-theme references.Follow this principle, not only can it ensure that the file is correctly imported, but it can also make the template structure clearer, even if the theme switching or structural adjustment is carried out in the later stage of the project, it can also greatly reduce the probability of errors.

includeEnhanced usage of tags

In addition to simple file references, AnQiCMS'sincludeTags also support some advanced usage that can make your template more flexible:

1. Pass variables (with):Sometimes, the public code introduced needs some specific data to work properly. You can usewithkeywords to pass variables to the introduced template. For example, if you want to give the introducedheader.htmla customtitleandkeywords:

{% include "partial/header.html" with title="我的网站首页" keywords="安企CMS,网站运营" %}

So, inheader.htmlYou can use it in{{ title }}and{{ keywords }}to display the passed value.

2. Isolate variables (only):If you only want the template included to use youwithPass the variable without inheriting all the context variables of the current template, you can useonlykeyword. This helps to avoid variable name conflicts and maintain the independence of code snippets:

{% include "partial/header.html" with title="我的网站首页" only %}

3. Safe reference (if_exists):In some cases, you are unsure whether the file to be imported exists or if it is an optional module. To avoid errors due to a missing file that could cause the page to crash, you can useif_existsKeyword. If the file does not exist,includethe tag will be ignored, and the page will still render normally:

{% include "partial/optional_ad.html" if_exists %}

MasterincludeThe method of specifying the file path of tags is a key step to improve the development efficiency and code quality of AnQiCMS templates. By reasonably organizing template files, especially by usingpartial/Directory and combinewith/only/if_existsWith enhanced features, you will be able to build more modular, easy to maintain, and consistent websites. Remember, clear path planning is the foundation of efficient work.

Frequently Asked Questions (FAQ)

**1. Q: Can I use../to refer to files outside the topic directory

Related articles

Which file path variables are available when customizing the static rules?

Hello! As an old soldier who has been deeply involved in website operations for many years, I know that a flexible and powerful static mechanism is crucial for the website's search engine optimization (SEO) and user experience.AnQiCMS (AnQi CMS) performs very well in this aspect, it not only provides a variety of out-of-the-box static mode, but also allows for deep customization of URL structure, which undoubtedly provides great convenience for refined operation.Today, let's delve into the discussion of which key file path variables we can flexibly use when customizing pseudo-static rules in Anqi CMS

2025-11-06

How to set up the background to view or modify the specific URL address of the website logo image?

As an experienced website operations expert, I am well aware of the importance of the website logo to the brand image.It is not only the 'front door' of the website, but also a key element for users to identify your brand.In a powerful content management system like AnQiCMS, managing the website logo image address is a fundamental and frequent operation.Today, let's delve into how to easily view or modify the specific URL address of the website logo image through backend settings.

2025-11-06

Where is the actual storage path of these image resources after I upload them to the background?

As an experienced website operation expert, I know the importance of image resource management for website performance, SEO, and even user experience.AnQiCMS (AnQiCMS) has made many meticulous designs in this aspect, allowing content operators to manage these valuable visual assets more efficiently.Today, let's delve deeply into a question that concerns everyone: 'Where are the actual storage paths of the image resources after I upload them to the backend?'}]

2025-11-06

How to distinguish the file path of `config.` for different sites in multi-site configuration?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, and its multi-site management function is undoubtedly a highlight favored by many operators.However, when it comes to multi-site configuration, some seemingly basic details, such as how to differentiate the `config.` file paths of different sites, often raise questions among users.Today, let's delve into this topic in depth to help everyone clearly understand the configuration logic of AnQiCMS under multi-site architecture.

2025-11-06

How to view the template file path of the system's default 404, 500 error pages and shutdown prompt page?

As an experienced website operations expert, I know that in the daily operations of a website, we always hope for everything to go smoothly, but unexpected situations are always hard to avoid.Whether it is the user visiting a non-existent page, or the backend server experiencing an occasional failure, or the website needing temporary maintenance, how to elegantly handle these special situations is directly related to user experience and brand image.AnQiCMS is well-versed in this, providing a highly flexible template mechanism to customize these key pages.Today, let's delve into how to find and personalize your 404 in AnQiCMS

2025-11-06

Where is the AnQiCMS scheduled task script (such as `start.sh`) usually stored?

As an experienced website operations expert, I know that the stable operation of every CMS system is inseparable from the silent support of its core components.Today, let's talk about a seemingly simple but crucial file in AnQiCMS - the scheduled task script `start.sh`, where is it hidden, and what role does it play in the daily operation and maintenance of AnQiCMS.--- ### Revealing AnQiCMS: The Hidden Location of the `start.sh` Scheduled Task Script For AnQiCMS users, whether it is for the first deployment or daily maintenance

2025-11-06

Can I view the path of the template file on the server directly through the AnQiCMS background online template editing feature?

As an experienced website operations expert, I am well aware of the importance of template management for website personalization and operational efficiency.AnQiCMS (AnQiCMS) boasts its high efficiency and flexibility, which is favored in the content management field. Its template editing function on the back-end provides great convenience for content operators.TodayUnlocking AnQiCMS Template Path

2025-11-06

How to get the full URL address of the current page being accessed in the template?

## Deep Analysis: How to cleverly get the complete URL address of the current page in Anqi CMS template As an experienced website operation expert, I am well aware that it is a common and important need to accurately obtain the complete URL address of the current page in daily work.In order to set the `canonical` tag, share on social media, pass parameters, or perform data statistics, the accurate URL of a page is core information.

2025-11-06