In the practical application of AnQi CMS, we often encounter the need to integrate third-party JavaScript code into the website, such as traffic statistics, online customer service, or advertisement code.There are usually two approaches presented to us at this point: one is to directly paste the JS code into the template file, and the other is to use the 'Statistics Code Tag' provided by the Anqi CMS.Both methods can achieve the goal, but there are significant differences between them. Understanding these differences will help us make more informed choices, thus optimizing the operation and maintenance of the website.
Method one: Directly paste JS code into the template file
When we choose to directly paste JS code into a template file, we usually find the public template files of the website, such as the ones responsible for the header (<head>), or the footer (</body>before the closing tag).bash.htmlor a specific pageindex.htmlin the Anqi CMS, template files are used.htmlSuffix, and supports Django template engine syntax, which means you can directly insert HTML, CSS, and JavaScript code into these files.
It seems direct and efficient; the advantages of this approach are evident: you have complete control and can precisely determine where the code is inserted.This is the most intuitive way for developers who are familiar with the template structure and need to perform detailed debugging.
However, the disadvantages of this approach are also prominent.Firstly, it requires direct modification of the template files on the server, which usually needs to be done through FTP or SSH connections, making the operation relatively cumbersome and not suitable for non-technical personnel.Secondly, when your website theme (template) is updated, the parts you directly modify may be overwritten by the new version, leading to the loss of JS code and requiring re-adding.This undoubtedly increases the maintenance cost, especially when the website is frequently updated or there are multiple sites.[en] Moreover, embedding a large amount of JS code directly in the template can easily lead to rendering errors of the entire page if there are syntax errors, even causing the website to crash, and it is also difficult to troubleshoot.From a security perspective, improper permission management of template files may also bring unnecessary risks.
Method two: Use the 'Statistics Code Tag' of Anqi CMS
AnQi CMS as an enterprise-level content management system, is committed to providing an efficient and scalable content management solution.It includes many convenient features, including a mechanism for managing third-party statistical codes.In the AnQi CMS backend, you can find the configuration item '360/Toutiao and other JS automatic submission' under 'Link Push' in 'Function Management'.Here, you can directly paste third-party JS code.
And in the template file, we only need to place a special 'statistics code tag' at the location where we need to introduce this code (usually also in the header or footer common template, likebash.html).{{- pluginJsCode|safe }}.
This method separates the management of JS code from the content editing of template files, bringing many advantages:
- Centralized management:All statistics or third-party JS code are unifiedly managed on the backend interface, making it clear at a glance, without the need to search for specific files.
- Theme update security:The JS code is not overwritten even if you update your website theme (template) because it is stored in the database and called through backend configuration, ensuring the continuity of website functionality.
- Operation simple:Non-technical personnel can easily add, modify, or delete JS code in the background, reducing the dependence on professional skills and improving operational efficiency.
- Avoid template errors:Code is stored through dedicated fields, the CMS will render through
|safefilters (fromtag-tongji.mdEnsure the code is safe as raw HTML/JS output to avoid errors due to syntax issues in templates.This greatly reduces the risk of website operation being affected by coding errors. - Background control is flexible:Combine other features of CMS, it may be possible to enable or disable JavaScript code on demand, or to configure differently for different sites and language environments, thus better supporting multi-site management and content operation strategies.
Core Differences and **Practice
The core differences between the twoManagement MethodsandCoupling Degree between Code and Templates
From the perspective of the advantages of the Anqi CMS project, its objectives such as 'lightweight, efficient content management services' and 'comprehensively improving the SEO performance of the website' all mean that we should make the most of the built-in powerful functions.Recommend using the "Statistics Code Tag" provided by Anqi CMS to integrate third-party JS code.This not only improves the maintenance efficiency and stability of the website, but also better adapts to the needs of team collaboration, and reduces the risk of improper operation by non-technical personnel.Only consider directly modifying template files in scenarios that require extreme precision control or temporary testing, and ensure that it will not have long-term negative impacts on the website.
In summary, as a user of the Anqi CMS, fully utilizing its built-in mechanisms, such as the 'Statistics Code Tag', is a **practical** way to improve website operation efficiency and ensure website stability and security.
Common Questions (FAQ)
1. If I want to include different JS code on different pages, can I still use the 'Statistics Code Tag'?“Statistics Code Tag” ({{- pluginJsCode|safe }}usually used to include global JS code, which will be executed in all templates referencing this tag (for example, all pages that inherit from it)bash.htmlActivated in【en】. If you need to introduce different JS code for specific pages or categories, you may need to consider directly pasting the JS code in the independent template files for these pages, or combining with the conditional judgment tags of the Anqi CMS template【en】.{% if ... %}), according to the page ID or category ID to introduce different JS fragments. But please note that the latter requires a deeper understanding of the template logic.
2. Will the JS code added using the 'Statistics Code Tag' be lost after updating the AnQi CMS version?Will not.The JS code added through the 'Statistics Code Tag' is stored in the database configuration of the Anqi CMS backend (for example, in the 'Link Push' feature's '360/Toutiao etc. JS Auto Submission' field).Therefore, neither the update of the system itself nor the update of the theme (template) will affect the JS code stored in the database.This is an important advantage of this method over directly modifying the template file.
3. In the 'Statistics Code Tag' of|safeWhat does it mean, and why is it important?
|safeis a filter of AnQin CMS template engine. By default, to prevent cross-site scripting attacks (XSS), the template engine will escape all output HTML tags and special characters (for example, changing<script>Escaped into<script>)。This means that if you directly output JS code, it may not be recognized as executable code by the browser.|safeThe function of the filter is to explicitly tell the template engine that this content is safe and does not require escaping; it should be output directly as raw HTML or JS code. For variables that contain HTML or JS code, using|safeIt is the key to ensure that they work properly.