menu_book Navigation menu

Global Helpers

Overview

These functions are globally available in the CMS backend and frontend (if the core JS is loaded). They bridge the gap between PHP configurations and JavaScript execution.

getTranslation(key, params, ucFirst, transform)

Retrieves a localized string from the globally injected window.CMS.translations object.

Example:

JAVASCRIPT
// Assuming the dictionary has: { "hello_name": "Hello {name}!" }
const greeting = getTranslation("hello_name", { name: "Alice" });
// Returns: "Hello Alice!"

getLink(routeHandle, params)

Generates a translated, relative URL matching the PHP routing logic.

Example:

JAVASCRIPT
const url = getLink("front.articles.show", { slug: "my-first-post" });
// Returns: "https://my-website.com/en/blog/my-first-post"

getAssetsPath(path, isTheme = false, extensionName = null)

Resolves an asset path following the CMS hierarchy (core → theme → extension).

Example:

JAVASCRIPT
const imageUrl = getAssetsPath("images/logo.png");
// Returns core, theme, or extension asset URL depending on context
JAVASCRIPT
const themeImage = getAssetsPath("images/bg.jpg", true);
// Forces resolution from active theme assets
JAVASCRIPT
const extensionImage = getAssetsPath("icons/icon.svg", false, "seo-extension");
// Resolves asset from a specific extension

escapeHtml(text)

A fast, OWASP-compliant string sanitizer to prevent Cross-Site Scripting (XSS) when injecting user data into the DOM.

Example:

JAVASCRIPT
const safeText = escapeHtml("<script>alert(1)</script>");
div.innerHTML = `<p>${safeText}</p>`;

slugify(text)

Transforms a string into a URL-friendly slug (lowercase, no special characters, replaces spaces with underscores).

copyToClipboard(selector, successMessage, errorMessage, callback)

Copies the text content (or input value) of a target element to the user's clipboard and automatically triggers a success/error popup.