Cookie Consent

Gate Scripts

Add data-consent to scripts that should wait for permission.

phpresources/views/layouts/default.php
<script src="analytics.js" data-consent="analytics"></script>

TemplateX rewrites the script during compilation. It does not run on first load. It runs only after the visitor allows the matching category.

External Scripts#

External scripts work the same way:

php
<script src="https://example.com/tracker.js" data-consent="marketing" async></script>

TemplateX keeps normal script attributes such as async.

Local Vite Scripts#

Local scripts still go through Vite:

php
<script src="analytics.js" data-consent="analytics"></script>

That maps to resources/js/analytics.js, just like a normal local script. CSS emitted by that Vite entry also waits for consent.

Inline Scripts#

Inline scripts can also be gated:

php
<script data-consent="analytics">  window.analyticsLoaded = true;</script>

The inline code is stored inertly and runs after consent.

Combine With Lazy Loading#

Use data-load-when and data-consent together when a script needs both matching markup and permission:

php
<script src="reviews-map.js" data-load-when=".reviews-map" data-consent="analytics"></script>

The script runs only after the visitor allows analytics and the page contains .reviews-map.