As an experienced website operations expert, I know the importance of an efficient content management system (CMS) for website operations.English CMS (EnglishCMS) provides a solid foundation for building various websites with its flexibility and powerful template functions.includeLabel, especially how the file path is specified when referencing public code snippets, is often a place where even experienced developers can be confused, especially for beginners.

Why choose to useincludeTags?

Imagine the header (header), footer (footer), or sidebar (sidebar) of our website, they almost appear on every page.If we had to copy and paste this code on every page, then every time we need to change a tiny link or a word, we would 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 appearing code snippets, save them as independent module files, and then simply reference them where needed.The benefits of doing this are obvious: the code is more organized, the structure is clearer, and it is also more efficient to maintain, ensuring a consistent visual and functional experience across different pages of the website.

Auto CMS Template File Organization Overview

All template files in Auto CMS are stored uniformly in/templateThe root directory. Each independent template theme, such as we may have a nameddefaultdefault theme or one you customizemy_themetheme, will have its own independent folder, such as/template/default.includeThe file path referred to by the tag is based on the current theme directory being used for parsing. This means that when you useincludeTags, the system will first look for the corresponding file in the folder of the theme.

To better organize public code snippets, Anqi CMS encourages storing them in the theme directory.partial/In the subdirectory. For example, in/template/default/partial/the directory, you might findheader.html/footer.html/sidebar.htmlThis convention not only makes the file structure clear at a glance, but also facilitates team collaboration and project management. If you need to create independent templates for mobile devices, then inmobile/In the subdirectory, you will also see similar.partial/The structure, the file organization inside it, is completely consistent with the PC theme.

How to specify the path to the imported file?

Understood the file organization structure, specifying the path becomes simple.includeIn the label, you can directly write the path relative to the current theme root directory. For example, to import the header file mentioned earlier, whether you are in/template/default/partial/header.html/template/default/index.htmlOr/template/default/archive/detail.htmlCiting, the paths should be written like this:

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

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

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

The key is always to consider your reference path as starting from the root directory of the currently active theme (for example/template/default/)start.The template engine of AnQi CMS will intelligently search for these files within the framework of the current theme, avoiding confusion that may arise from complex absolute paths or cross-theme references.Follow this principle, not only can it ensure that the file is correctly introduced, but it also makes the template structure clearer. Even if the theme switching or structural adjustment is made during the later stage of the project, it can greatly reduce the probability of errors.

includeEnhanced usage of tags

In addition to simple file references, AnQiCMS'sincludeThe label supports some advanced usage to 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 pass to the introducedheader.htmlPass a customtitleandkeywords:

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

Then, inheader.htmlcan be used after that{{ title }}and{{ keywords }}to display the passed value.

2. Isolated variables (only):If you only want the imported templates to use yourwithPass the variable without inheriting all context variables of the current template, which can be done usingonlykeywords. This helps to avoid variable name conflicts and maintain the independence of code snippets

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

3. Secure reference (if_exists):In some cases, you are not sure if the file to be introduced exists, or it is an optional module. To avoid errors on the page due to the non-existence of the file, 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 file path specification method of the label is a key step to improve the development efficiency and code quality of AnQiCMS templates. By organizing the template files reasonably, especially by making good use ofpartial/Catalog, and combinewith/only/if_existsEnhanced features, you will be able to build a more modular, easy to maintain, and consistent website. Remember, clear path planning is the foundation of efficient work.

Common Questions (FAQ)

**1. Q: Can I use../to reference files outside the topic catalog