Skip to content

Commit

Permalink
Smarty 3/4 - Fix prepending extension template directories
Browse files Browse the repository at this point in the history
Before: Extensions would prepend their directories under smarty2, but smarty3 & 4 would append them, breaking template overrides.

After: Smarty 2,3&4 all prepend template directories

Fixes civicrm/org.civicrm.contactlayout#143
  • Loading branch information
colemanw committed May 8, 2024
1 parent 43642fe commit 2b2dfac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 16 additions & 4 deletions mixin/smarty-v2@1/mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Auto-register "templates/" folder.
*
* @mixinName smarty-v2
* @mixinVersion 1.0.1
* @mixinVersion 1.0.2
* @since 5.59
*
* @deprecated - it turns out that the mixin is not version specific so the 'smarty'
Expand All @@ -23,9 +23,21 @@
}

$register = function() use ($dir) {
// This implementation has a theoretical edge-case bug on older versions of CiviCRM where a template could
// be registered more than once.
CRM_Core_Smarty::singleton()->addTemplateDir($dir);
$smarty = CRM_Core_Smarty::singleton();
// Smarty2 compatibility
if (isset($smarty->_version) && version_compare($smarty->_version, 3, '<')) {
$smarty->addTemplateDir($dir);
return;
}
// getTemplateDir returns string or array by reference
$templateRef = $smarty->getTemplateDir();
// Dereference and normalize as array
$templateDirs = (array) $templateRef;
// Add the dir if not already present
if (!in_array($dir, $templateDirs, TRUE)) {
array_unshift($templateDirs, $dir);
$smarty->setTemplateDir($templateDirs);
}
};

// Let's figure out what environment we're in -- so that we know the best way to call $register().
Expand Down
20 changes: 16 additions & 4 deletions mixin/smarty@1/mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Auto-register "templates/" folder.
*
* @mixinName smarty
* @mixinVersion 1.0.0
* @mixinVersion 1.0.2
* @since 5.71
*
* @param CRM_Extension_MixInfo $mixInfo
Expand All @@ -19,9 +19,21 @@
}

$register = function() use ($dir) {
// This implementation has a theoretical edge-case bug on older versions of CiviCRM where a template could
// be registered more than once.
CRM_Core_Smarty::singleton()->addTemplateDir($dir);
$smarty = CRM_Core_Smarty::singleton();
// Smarty2 compatibility
if (isset($smarty->_version) && version_compare($smarty->_version, 3, '<')) {
$smarty->addTemplateDir($dir);
return;
}
// getTemplateDir returns string or array by reference
$templateRef = $smarty->getTemplateDir();
// Dereference and normalize as array
$templateDirs = (array) $templateRef;
// Add the dir if not already present
if (!in_array($dir, $templateDirs, TRUE)) {
array_unshift($templateDirs, $dir);
$smarty->setTemplateDir($templateDirs);
}
};

// Let's figure out what environment we're in -- so that we know the best way to call $register().
Expand Down

0 comments on commit 2b2dfac

Please sign in to comment.