In the actual 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.This is usually faced with two methods in front of us: one is to directly paste JS code into the template file, and the other is to use the "statistical code tag" provided by AnQi CMS.Both methods can achieve the purpose, but there are significant differences between them. Understanding these differences will help us make more informed choices and optimize the operation and maintenance of the website.

Method 1: Directly paste the 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 those responsible for the header (<head>), or the footer (</body>before the closing tag).bash.htmlor a specific pageindex.htmlTemplate files are used in Anq CMS..htmlThe suffix supports Django template engine syntax, which means you can directly insert HTML, CSS, and JavaScript code into these files.

It seems direct and efficient, and the advantages of this approach are obvious: you have complete control and can precisely determine the position where the code is inserted.For developers who are familiar with template structures and need to perform detailed debugging, this seems to be the most intuitive way.

However, the disadvantages of this approach are also prominent. Firstly, it requires direct modification of the template files on the server, which usually requires a connection through FTP or SSH, the operation is relatively cumbersome, and it is 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, causing the JS code to be lost and requiring it to be added again.This undoubtedly increases the maintenance cost, especially when the website is frequently updated or there are multiple sites.Moreover, directly embedding a large amount of JS code in the template can easily lead to rendering anomalies on the page, even causing the website to crash, which is also difficult to troubleshoot.From a security perspective, improper management of template file permissions may also bring unnecessary risks.

Method two: Using 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 code.In the Anqi CMS backend, you can find the configuration item for '360/Top Stories etc. JS automatic submission' under 'Link Push' settings in 'Function Management'.Here, you can directly paste third-party JS code in.

In the template file, we only need to place a special 'statistical code tag' at the positions where we need to include these codes (usually also in the header or footer public templates, likebash.html),placing a special 'statistical code tag':{{- pluginJsCode|safe }}.

This method separates the management of JS code from the content editing of template files, bringing many advantages:

  1. Centralized management:All statistics or third-party JS code are unified in the background interface for management, making it clear at a glance, no need to search for specific files.
  2. Theme update security:Because the code is stored in the database and called through the background configuration, these JS codes will not be overwritten even if the theme (template) of your website is updated, ensuring the continuity of website functions.
  3. Operation is simple:Non-technical personnel can easily add, modify, or delete JS code in the background, reducing the dependency on professional skills and improving operational efficiency.
  4. Avoid template errors:Code is stored through a dedicated field, the CMS will pass through it during rendering|safeFilter (fromtag-tongji.mdEnsure that the code is output as raw HTML/JS to avoid any syntax errors when directly outputting in the template.This greatly reduces the risk of website operation failure due to coding errors.
  5. Back-end control is flexible:Combine the other features of CMS, it may be possible to enable or disable JS code on demand, or to differentiate configuration for different sites and language environments, thus better supporting multi-site management and content operation strategy.

Core difference and **practice

The core difference between the two lies inManagement methodandCoupling degree between code and template. Placing code directly next to the template file tightly couples JavaScript, leading to scattered management and a high impact from theme updates;Using the 'Statistics Code Tag' stores the JS code as configuration data, decouples it from the template file, centralizes management, is less affected by theme updates, and is more secure and convenient.

From the perspective of the advantages of the AnQi CMS project, its goals such as 'lightweight, efficient content management services' and 'comprehensive improvement of the website's SEO performance' all mean that we should make full use of the powerful built-in functions of the system. Therefore,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 non-technical personnel making mistakes.Only in rare cases where extreme fine control or temporary testing is needed, and it is confirmed that it will not have a long-term negative impact on the website, should you consider cautiously modifying the template file directly.

In summary, as a user of Anqi CMS, making full use of its built-in mechanisms, such as the 'statistics code tag', is a practice to enhance website operation efficiency, ensure website stability and security.


Frequently Asked Questions (FAQ)

1. If I want to introduce different JS code on different pages, can I still use the 'Statistics Code Tag'?“Statistics Code Tag” ({{- pluginJsCode|safe }}) Typically used to include global JS code, which will be executed in all templates referencing this tag (for example, all inherited pages)bash.html)works. If you need to introduce different JS code for specific pages or categories, you may need to consider directly pasting the JS code into the independent template files of these pages or combining with the conditional judgment tags of Anqi CMS template({% if ... %}Please note that the latter requires a deeper understanding of the template logic to introduce different JS fragments based on page ID or category ID.

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 Automatic Submission' field).Therefore, the update of the system itself or the theme (template) update will not affect these JS codes stored in the database.This is an important advantage of this method compared to directly modifying the template file.

3. In the 'Statistics Code Tag'|safeWhat does it mean, and why is it important? |safeIs an Anqi CMS template engine filter. By default, to prevent cross-site scripting attacks (XSS), the template engine will escape all output HTML tags and special characters (such as the<script>Escape to&lt;script&gt;This means that if you directly output JavaScript code, it may not be recognized as executable by the browser.|safeThe filter's role is to clearly inform the template engine that this content is safe, does not need to be escaped, and should be output as raw HTML or JS code. For variables containing HTML or JS code, it is recommended to use|safeIt is the key to ensure that they work properly.