How to specify the file path of the public code snippet included when using the `include` tag in the template?
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