docs/design/theme-editor.md
{% hint style="info" %} Template Code Editing 🎨 The Theme Editor allows you to directly edit template files using the Twig templating language. Make live changes to your store's appearance without accessing server files. {% endhint %}
{% embed url="https://youtu.be/some-id" %}
Video: Using the Theme Editor in OpenCart 4
The Theme Editor in OpenCart 4 provides a web‑based interface for editing template files directly from the admin panel. It supports the Twig templating language and lets you modify HTML structure, add custom code, and customize your store’s appearance without FTP access or direct file system modifications. All changes are stored in the database, allowing you to revert or disable them at any time.
The Theme Override list shows all template overrides currently stored in the database. From here you can:
Each entry displays:
common/header, product/product){% hint style="info" %}
Override vs. Physical File 📝 Theme Editor overrides are stored in the database, not as physical .twig files. The original template file on disk remains untouched, making it safe to experiment.
{% endhint %}
When you create or edit a theme override, you fill out a form with the following fields:
.twig files from catalog/view/template/extension/*/catalog/view/template/){% hint style="success" %} Code Editor Features đź”§ The editor includes syntax highlighting for Twig, HTML, CSS, and JavaScript, line numbers, and a monokai color scheme. It also supports automatic indentation and bracket matching. {% endhint %}
OpenCart 4 uses a template fallback system:
catalog/view/template/product/product.twig).theme database table for an enabled override that matches the current store and route.code is used instead of the file content.This mechanism allows you to customize any template without touching the core files, making upgrades safer and reversible.
OpenCart 4 organizes templates in a hierarchical structure:
| Template Type | Location | Description |
|---|---|---|
| Store Templates | catalog/view/template/ | Frontend store templates |
| Admin Templates | admin/view/template/ | Admin panel templates |
| Extension Templates | extension/*/view/template/ | Extension‑specific templates |
| Theme Templates | catalog/view/theme/*/template/ | Theme‑specific overrides |
{% hint style="info" %}
Note: The Theme Editor only works with store templates (catalog/view/template/) and extension templates. Admin templates cannot be overridden via the Theme Editor.
{% endhint %}
OpenCart 4 uses Twig as its template engine. Below are the essential concepts you need to edit templates successfully.
<details> <summary><strong>Variables & Output</strong></summary>{# Display a variable #}
<h1>{{ heading_title }}</h1>
<p>{{ description }}</p>
{# Display with filters #}
<p>{{ text|upper }}</p>
<p>{{ price|number_format(2) }}</p>
Common Variables:
{{ heading_title }} – Page title{{ description }} – Page description{{ products }} – Array of products{{ currency }} – Currency information{# If statement #}
{% if products %}
<ul>
{% for product in products %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
{% else %}
<p>No products found.</p>
{% endif %}
{# For loops #}
{% for category in categories %}
<a href="{{ category.href }}">{{ category.name }}</a>
{% endfor %}
{# Include another template #}
{% include 'common/header.twig' %}
{# Extend a base template #}
{% extends 'common/base.twig' %}
{# Override blocks #}
{% block content %}
Custom content here
{% endblock %}
Workflow Guidelines
1. Test Locally First Always test changes on a development or staging store before applying them to a live site.
2. Make Small, Incremental Changes Edit one template at a time and verify each change works as expected.
3. Document Your Changes Keep notes on which templates you modified, what you changed, and why. This helps when troubleshooting or upgrading.
4. Use Version Control Consider using Git for your template overrides, especially if you have many customizations.
5. Regular Backups Back up your theme overrides (export the theme table) before and after major changes.
Safe Template Editing
1. Sanitize User Input Always escape user‑generated content with Twig’s |escape filter to prevent XSS attacks.
2. Never Embed PHP Code Twig templates are not meant to contain PHP code. Use Twig’s built‑in functions and filters instead.
3. Restrict Access Limit Theme Editor access to trusted administrators only (via User Groups).
4. Code Review Review template changes for potential security issues, especially when adding custom JavaScript or form handling.
{% hint style="warning" %}
Warning: Avoid using {{ variable|raw }} unless you absolutely trust the source of the variable. This can expose your store to cross‑site scripting (XSS) attacks.
{% endhint %}
Managing Overrides Across Stores
Store‑Specific Overrides You can create different overrides for each store in a multi‑store setup. Simply select the target store when creating the override.
Language Considerations Template overrides are language‑agnostic; they affect the template structure, not the text content. For text changes, use the Language Editor.
Fallback Behavior If a store does not have a specific override, OpenCart falls back to the default store’s template (or the original file).
</details>{% stepper %} {% step %}
{% hint style="info" %} Quick Load: When you select a template, the editor automatically loads the current template code. You can modify it or start from scratch. {% endhint %} {% endstep %}
{% step %}
{% hint style="warning" %} Caution: Editing an active override will immediately change the live storefront. Consider disabling the override first if you want to test changes in isolation. {% endhint %} {% endstep %}
{% step %}
Alternative: You can also delete the override and recreate it later. Disabling is non‑destructive and preserves your code. {% endstep %}
{% step %}
{% hint style="info" %} No “Revert” Button: The Theme Editor does not have a built‑in revert button. You must manually restore the original code or disable the override. {% endhint %} {% endstep %} {% endstepper %}
{% hint style="danger" %} Critical Warnings
theme table is included in the backup.Problem: Override saved but storefront shows original template
Diagnostic Steps:
Quick Solutions:
Problem: White screen or error message after saving
Diagnostic Steps:
{% endfor %}, {% endif %}, or unmatched {{ }}.Quick Solutions:
Problem: Storefront layout is distorted
Possible Causes:
<div>).Solution:
.twig file and compare it with your override.Problem: Override works on one store but not another
Diagnostic Steps:
Quick Solutions:
{% hint style="info" %} Advanced Theme Development 🛠️ For complex theme modifications beyond simple template overrides:
catalog/view/theme/yourtheme/