Technical Library / Open Source

Joomla Snippets

Searchable collection of production-ready code recipes for template overrides, modules, plugins, and Joomla! 3 to 6 APIs.

100 Snippets
12 Categories
J3 to J6 Compatibility
Showing 6 of 6 snippets
JavaScript J6 J5 J4 Template
JavaScript, CSS and SEO

Pass Safe PHP Data to JavaScript with addScriptOptions() and Joomla.getOptions()

Replaces unsafe inline JS globals with CSP-compliant JSON configuration via $doc->addScriptOptions() and Joomla.getOptions().

// No PHP: $doc->addScriptOptions('com_example.config', ['endpoint' => $url, 'limit' => 12]);
document.addEventListener('DOMContentLoaded', () => {
  const cfg = Joomla.getOptions('com_example.config', {
    endpoint: '',
    limit: 10,
  });

// ...
58 193
JavaScript J6 J5 J4 Template
JavaScript, CSS and SEO

Translate Frontend JavaScript Strings with Text::script() and Joomla.Text._()

Exposes .ini language constants from PHP with Text::script() and resolves localized labels cleanly using Joomla.Text._().

// No PHP: Text::script('COM_EXAMPLE_COPIED');
//         Text::script('COM_EXAMPLE_RESULTS_FMT');
const copiedLabel = Joomla.Text._('COM_EXAMPLE_COPIED', 'Copiado!');

// Substituição de placeholders %d / %s
const rawSummary = Joomla.Text._('COM_EXAMPLE_RESULTS_FMT', 'Exibindo %d de %d');
const summary    = rawSummary.replace('%d', String(12)).replace('%d', String(100));
48 163
JavaScript J6 J5 J4 Component
JavaScript, CSS and SEO

Make CSRF-Protected AJAX Calls with Native Joomla.request()

Sends asynchronous POST requests with automatic CSRF headers and JsonResponse parsing using Joomla native core wrapper.

async function sendVote(endpointUrl, itemId) {
  const token = Joomla.getOptions('csrf.token', '');
  const body  = new URLSearchParams({ id: String(itemId) });
  if (token) {
    body.set(token, '1');
  }

// ...
53 179
JavaScript J6 J5 J4 Template
JavaScript, CSS and SEO

Display Dynamic System Alerts via JavaScript with Joomla.renderMessages()

Renders native accessible alerts (success, warning, error, info) inside #system-message-container without a page reload.

// Exibe notificação de sucesso no container padrão do template
Joomla.renderMessages({
  success: ['Configurações salvas com sucesso!'],
});

// Limpa mensagens anteriores e exibe erro dentro de um container específico
Joomla.removeMessages(document.querySelector('#meu-form-alerts'));
// ...
39 135
PHP J6 J5 J4 Template Override
JavaScript, CSS and SEO

Inject Structured Schema.org JSON-LD into <head> via HtmlDocument

Outputs valid Article, FAQPage or SoftwareSourceCode structured data in application/ld+json for Google Rich Results.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// ...
55 188
PHP J6 J5 J3 Template Override
JavaScript, CSS and SEO

Set Canonical Link, Open Graph and Twitter Cards Dynamically

Outputs rel="canonical", og:title, og:description, og:image and twitter:card cleanly through HtmlDocument for optimal SEO and sharing.

<?php

declare(strict_types=1);

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// ...
60 203 2 versions
Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.