- {if $counts.$cluster|@count gt 6}
+ {if $counts.$cluster|count gt 6}
diff --git a/static/api/templates/volumes/oclchtml.tpl b/static/api/templates/volumes/oclchtml.tpl
index 149dd2b1..e9bd225c 100644
--- a/static/api/templates/volumes/oclchtml.tpl
+++ b/static/api/templates/volumes/oclchtml.tpl
@@ -3,7 +3,10 @@
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- {$doc.titles[0]} (OCLC {', '|implode:$doc.oclcs })
+
+ {$doc.titles[0]|default:''} {if !empty($doc.oclcs|default:[])} (OCLC {$doc.oclcs|join:', '}){/if}
+
+
diff --git a/static/api/volumes.php b/static/api/volumes.php
index ccae61c5..2d97e3a6 100644
--- a/static/api/volumes.php
+++ b/static/api/volumes.php
@@ -16,6 +16,7 @@
# require_once 'Apache/Solr/Service.php';
require_once 'vendor/autoload.php';
+use Smarty\Smarty;
require_once 'sys/SolrConnection.php';
require_once 'services/Record/RecordUtils.php';
require_once 'sys/Normalize.php';
@@ -629,4 +630,3 @@ function lccnnormalize($val) {
-
diff --git a/sys/Interface.php b/sys/Interface.php
index ee426403..08ff2edb 100755
--- a/sys/Interface.php
+++ b/sys/Interface.php
@@ -18,10 +18,9 @@
*
*/
-# Smarty 4.5.5 uses classmap autoload with bare Smarty class
require_once 'vendor/autoload.php';
-# use Smarty\Smarty;
+use Smarty\Smarty;
# Smarty Extension class
class UInterface extends Smarty
@@ -33,6 +32,7 @@ function __construct()
$local = $configArray['Site']['local'];
$theme = $configArray['Site']['theme'];
+ # set a single directory where the config file are stored.
$this->setTemplateDir("$local/interface/themes/$theme");
# Set up the space for compiled files
@@ -44,10 +44,13 @@ function __construct()
chmod($comp, 0777);
}
+ // set another path to store compiled templates
$this->setCompileDir($comp);
+
+ // set another path to store caches of templates to speed up the loading of templates
$this->setCacheDir("$local/interface/cache");
- # Add custom plugin directory
- $this->addPluginsDir("$local/interface/plugins");
+ # Register legacy plugin files from the custom directory
+ $this->registerLegacyPlugins("$local/interface/plugins");
$this->setCaching(Smarty::CACHING_OFF);
$this->setDebugging(false);
$this->setCompileCheck(Smarty::COMPILECHECK_ON);
@@ -55,10 +58,20 @@ function __construct()
unset($local);
- // Register custom functions (Smarty 3 method)
+ // Register custom functions
+ // These are used in the templates as {translate text="Back to Record"} to output the translated text
+ // registerPlugin documentation: https://www.smarty.net/docs/en/api.register.plugin.tpl
$this->registerPlugin('function', 'translate', 'translate');
$this->registerPlugin('function', 'char', 'char');
+ // Register PHP functions that used to be invoked via Smarty's @modifier syntax.
+ // These are used in the templates as {$var|json_encode="value"} to output the JSON-encoded value of $var,
+ // or {$array|count} to output the count of items in $array.
+ // modifier are used to transform the output of a variable, so they are registered as 'modifier' plugins.
+ $this->registerPlugin('modifier', 'json_encode', 'json_encode');
+ $this->registerPlugin('modifier', 'count', 'count');
+
+
$this->assign('site', $configArray['Site']);
$this->assign('path', $configArray['Site']['path']);
$this->assign('url', $configArray['Site']['url']);
@@ -105,6 +118,42 @@ function setPageTitle($title)
{
$this->assign('pageTitle', $title);
}
+
+ /**
+ * Registers legacy plugins from a specified directory.
+ *
+ * This method looks for PHP files in the given directory that match the naming convention
+ * for Smarty plugins (function.{name}.php or modifier.{name}.php). It then includes these files
+ * and registers the corresponding functions as Smarty plugins.
+ *
+ * Smarty 5 requires to register each plugin or load an extension instead of pointing it at a directory.
+ *
+ *
+ * @param string $directory The directory to search for legacy plugin files.
+ */
+
+ private function registerLegacyPlugins(string $directory): void
+ {
+ if (!is_dir($directory)) {
+ return;
+ }
+
+ //foreach (glob(rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.php') as $file) {
+ foreach (glob($directory . '/*.php') as $file) {
+ require_once $file;
+ $basename = basename($file);
+ if (!preg_match('/^(function|modifier)\\.([^\\.]+)\\.php$/', $basename, $matches)) {
+ continue;
+ }
+ $type = $matches[1];
+ $name = $matches[2];
+ $callback = 'smarty_' . $type . '_' . $name;
+ if (!function_exists($callback)) {
+ continue;
+ }
+ $this->registerPlugin($type, $name, $callback);
+ }
+ }
}
function translate($params)
diff --git a/test/SmartyTemplateTest.php b/test/SmartyTemplateTest.php
index d01b542f..8bb95d33 100644
--- a/test/SmartyTemplateTest.php
+++ b/test/SmartyTemplateTest.php
@@ -2,6 +2,7 @@
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
+use Smarty\Smarty;
class VolumesApiTest extends TestCase
{
@@ -15,10 +16,10 @@ public function test_oclcscrape_smarty_template_rendering(): void
// Setup environment
$_SERVER['HTTP_HOST'] = 'localhost';
- // Verify Smarty 4.x class is available (no namespace)
- $this->assertTrue(class_exists('Smarty'), 'Smarty class should be available without namespace');
+ // Verify the namespaced Smarty class loads
+ $this->assertTrue(class_exists(Smarty::class), 'Smarty\\Smarty class should be available');
- // Create Smarty instance using Smarty 4.x syntax
+ // Create Smarty instance using the namespaced class
$interface = new Smarty();
$interface->setCompileDir(__DIR__ . '/../interface/compile');
$interface->setTemplateDir(__DIR__ . '/../static/api/templates');
@@ -47,4 +48,4 @@ public function test_oclcscrape_smarty_template_rendering(): void
$this->assertStringContainsString('https://localhost:8080/item', $output);
}
}
-?>
\ No newline at end of file
+?>