Skip to content

Commit

Permalink
Merge pull request civicrm#30148 from colemanw/smartyTemplateDir
Browse files Browse the repository at this point in the history
Smarty 3/4 - Fix prepending extension template directories
  • Loading branch information
totten authored and eileenmcnaughton committed May 8, 2024
2 parents 5a41d7a + 2b2dfac commit 0079088
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
23 changes: 18 additions & 5 deletions CRM/Core/Smarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ private function initialize() {
$smartyPluginsDir = defined('SMARTY_PLUGINS_DIR') ? SMARTY_PLUGINS_DIR : ($pkgsDir . DIRECTORY_SEPARATOR . 'Smarty' . DIRECTORY_SEPARATOR . 'plugins');
$corePluginsDir = __DIR__ . DIRECTORY_SEPARATOR . 'Smarty' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR;

if ($customPluginsDir) {
$this->plugins_dir = [$customPluginsDir, $smartyPluginsDir, $corePluginsDir];
}
else {
$this->plugins_dir = [$smartyPluginsDir, $corePluginsDir];
if ($this->getVersion() !== 5) {
if ($customPluginsDir) {
$this->plugins_dir = [$customPluginsDir, $smartyPluginsDir, $corePluginsDir];
}
else {
$this->plugins_dir = [$smartyPluginsDir, $corePluginsDir];
}
}

$this->compile_check = $this->isCheckSmartyIsCompiled();
Expand Down Expand Up @@ -522,4 +524,15 @@ public static function escape($string, $esc_type = 'html', $char_set = 'UTF-8')
return $value;
}

/**
* Get the smarty version - smarty 5 overrides this - otherwise we aren't quite sure.
*
* @todo figure out how to identify the other versions & reply accurately.
*
* @return int|null
*/
public function getVersion(): ?int {
return NULL;
}

}
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 0079088

Please sign in to comment.