Lesson 3.2: Override rules in override.ini
An override says: "for this class, this node or this section, use my template instead of the default". It has two
parts, a template file and a rule in override.ini. In this lesson learners write both by hand, inside their
extension, so the change is under version control and can be undone by removing two files.
At a glance
| Course | Developers |
| Module | Module 3: Designs, overrides and debugging |
| Duration | 60 minutes (20 presenting, 40 exercise) |
| Level | Intermediate |
| Audience | Developers and template builders |
| Prerequisites | Lesson 3.1: mysite_hello is active and has a design folder |
Learning objectives
After this lesson, learners can:
- write an override block with
Source,MatchFile,SubdirandMatch[...]; - choose a match key:
class_identifier,node,object,section,parent_node,viewmodeand others; - place the template in
design/<design>/override/templates/; - clear the
template-override, template and content caches after adding a rule; - explain how
Prioritydecides between several matching rules.
Trainer's notes
How to present it
Source: Templates and design, part 4.
An example rule (5 minutes). Project this block and read it line by line:
[full_article_course] Source=node/view/full.tpl MatchFile=full/article_course.tpl Subdir=templates Match[class_identifier]=articleSourceis the template being replaced,MatchFilethe new file relative tooverride/templates/,Subdiris alwaystemplates, and everyMatch[...]must hold.Match keys (5 minutes). The template editor offers
class_identifier,class,node,object,section,section_identifier,remote_id,node_remote_id,parent_node,class_group,depth,url_alias,viewmode,navigation_part_identifier,persistent_variable,stateandstate_identifier. Since 6.0.15 a node view also has the keylayout(empty unlesslayout/setis used).Where the rule lives (4 minutes). In
settings/siteaccess/<sa>/override.ini.append.phpfor one siteaccess, or in an extension'ssettings/siteaccess/<sa>/override.ini.append.php. Keeping it in the extension ships the rule with its template.Order (3 minutes). Rules with a
Priorityare tried from the lowest number up; rules without aPrioritycome after them. The first rule whose conditions all hold wins.Caches (3 minutes). A new rule or a new template path needs
template-overrideand the template caches cleared; pages already in the content view cache need--clear-tag=content.
Common questions
How do I override for two classes? Two blocks, one per class. Conditions within one block are combined with "and".
Can I override a line view only inside one folder? Yes: Source=node/view/line.tpl with
Match[parent_node]=<node ID>.
What is in the template? The same variables as the original: in a node view, $node and the view parameters.
Start from a copy of the original.
Pitfalls
MatchFilewith theoverride/templates/prefix. It is relative tooverride/templates/; writefull/article_course.tpl.- A block name that exists elsewhere. Block names must be unique across all
override.inifiles of the siteaccess; a duplicate name merges with the other block. Prefix with your extension's name. - Forgetting the siteaccess folder. A rule in the extension's
settings/override.ini.append.php, outside a siteaccess folder, is meant for every siteaccess, the administration included. Put a site's rules in the extension'ssettings/siteaccess/<sa>/folder, as in the exercise, and check withexp:ini where override.ini/<block>/MatchFile <sa>which files define a block.
Hands-on exercise
On the development installation, with mysite_hello active from lesson 3.1. <sa> is your public siteaccess,
<design> its site design.
Copy the kernel's full view as a starting point:
mkdir -p extension/mysite_hello/design/<design>/override/templates/full cp design/standard/templates/node/view/full.tpl extension/mysite_hello/design/<design>/override/templates/full/mysite_article.tplAdd a visible line at the top of the copy:
<p class="mysite-note">Course override: {$node.name|wash}</p>.Create the rule with
exp:iniin the extension's siteaccess scope:php bin/php/console exp:ini set override.ini/mysite_full_article/Source node/view/full.tpl extension:mysite_hello:siteaccess:<sa> php bin/php/console exp:ini set override.ini/mysite_full_article/MatchFile full/mysite_article.tpl extension:mysite_hello:siteaccess:<sa> php bin/php/console exp:ini set override.ini/mysite_full_article/Subdir templates extension:mysite_hello:siteaccess:<sa> php bin/php/console exp:ini set "override.ini/mysite_full_article/Match[class_identifier]" article extension:mysite_hello:siteaccess:<sa>Open the file that was written,
extension/mysite_hello/settings/siteaccess/<sa>/override.ini.append.php, and compare it with the example block above.Check the syntax of the template:
php bin/php/eztemplatecheck.php extension/mysite_hello/design/<design>/override/templates/full/mysite_article.tpl.Clear the caches:
php bin/php/ezcache.php --clear-tag=ini php bin/php/ezcache.php --clear-id=template-override php bin/php/ezcache.php --clear-tag=template php bin/php/ezcache.php --clear-tag=contentOpen an article on the site. The line from step 2 appears.
Open a folder: the line does not appear.
Narrow the rule to one article: add
Match[node]with that article's node ID the same way, clear theini,template-overrideandcontentcaches, and check another article.Commit the template and the settings file.
Expected result
- Step 4: a block
[mysite_full_article]with the four lines, inside the<?php /* ... */ ?>wrapper of an.ini.append.phpfile. - Step 7: the article page starts with "Course override:" and its name; step 8: folders are unchanged.
- Step 9: only the chosen article shows the line.
- The trainer checks by opening one learner's article with template path comments on (lesson 3.4).
Quiz
- What does
Sourcename in an override block? MatchFile=full/article_course.tplis relative to which folder? A.templates/B.override/templates/C.design/D.settings/- Two conditions in one block: are they combined with "and" or "or"?
- Which caches must be cleared after adding a new rule and template?
- Two rules match; one has
Priority=10, the other none. Which wins?
Answers
- The template being replaced, for example
node/view/full.tpl. - B.
override/templates/of a design in the chain. - "And": every
Match[...]must hold. ini(for the new rule),template-override,template, andcontentfor pages already cached.- The rule with
Priority=10: rules without a priority sort last (Template editor).
Further reading
- Templates and design: overrides by click and by file, the
layoutkey. - Template editor: match keys, priorities and how the editor writes rules.
- Extension module override: overriding a module's templates from an extension.
Related pages
- Module 3: Designs, overrides and debugging
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.