Allow themes to inherit module templates#1174
Allow themes to inherit module templates#1174jaimeperez merged 3 commits intosimplesamlphp:masterfrom
Conversation
|
Hey Guy, How is this different from the already existing functionality? |
That's not quite the same thing - it is including content from within the same module and with a different name. That namespace, as you correctly point out, already exists as To replicate the problem I am trying to solve with that example you would need to theme the admin module such that you had a file:
being a theme that replaces:
from your theme. In your theme, you then try and include the original However, that doesn't work and creates a circular reference back to |
|
Make sense now! Thanks for the explanation! |
|
Thanks for the further explanation Guy! I was confused as well about this. It's indeed such a useful addition! |
|
Hi @ghalse! We've noticed a bug that looks like it was introduced by this PR: #1312. Since I don't want to break your feature by fixing the bug, would you mind applying this patch and telling me if everything keeps working as expected? Thanks in advance! diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 24986d32f..25371a924 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -250,14 +250,15 @@ class Template extends Response
$loader = new TemplateLoader();
$templateDirs = $this->findThemeTemplateDirs();
if ($this->module && $this->module != 'core') {
- $templateDirs[] = [$this->module => TemplateLoader::getModuleTemplateDir($this->module)];
+ $modDir = TemplateLoader::getModuleTemplateDir($this->module);
+ $templateDirs[] = [$this->module => $modDir];
+ $templateDirs[] = ['__parent__' => $modDir];
}
if ($this->theme['module']) {
try {
$templateDirs[] = [
$this->theme['module'] => TemplateLoader::getModuleTemplateDir($this->theme['module'])
];
- $templateDirs[] = ['__parent__' => TemplateLoader::getModuleTemplateDir($this->module)];
} catch (\InvalidArgumentException $e) {
// either the module is not enabled or it has no "templates" directory, ignore
} |
When developing themes it is common to want to make only minor cosmetic changes to the existing template, such as adding additional CSS. Under the old theming system it was possible to inherit the stock theme and alter it by simply include()ing the PHP.
Similar functionality doesn't exist in Twig, but arguably makes more sense here where we can now override a single block. This functionality is already used by themes to override the default theme, but the model doesn't extend to modules. Thus this commit introduces a special
__parent__namespace in Twig to allow theme developers to reference the original module templates from within their own theme's template.E.g if I wanted to inheret the entire discopower module's template save for injecting one additional script into it, rather than copying the whole
disco.twigtemplate to my theme and editing it, I can merely override the postload block by creating a newmymodule/themes/mytheme/discopower/disco.twiglike this:That's a whole lot less maintenance for me as a theme developer when the module's template changes.