From 51fcda227a5537a15358d833c88c33dc9876a3d9 Mon Sep 17 00:00:00 2001 From: Isabelle Date: Thu, 20 Nov 2025 09:53:31 +0100 Subject: [PATCH] feat(extra-features): Add svg icon and price splitter smarty functions These are just helpers and useful for many prestashop designs --- .../src/Core/Smarty/SmartyHelperFunctions.php | 75 +++++++++++++++++++ is_themecore/src/Hook/Smarty.php | 10 +++ 2 files changed, 85 insertions(+) diff --git a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php index a28ea5f..271d74c 100644 --- a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php +++ b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php @@ -195,4 +195,79 @@ class SmartyHelperFunctions $calculator = new ColorBrightnessCalculator(); return $calculator->isBright($hexColor); } + + /* Helper to display SVG icons stored in themes/falcon/_dev/img/icons/ + + Registered under public_html/config/smartyfront.config.inc.php + + Usage examples in a TPL file: + + {svg_icon file='person.svg' color='red' width='24' height='24'} + {svg_icon file='person.svg' color='#3498db'} + {svg_icon file='person.svg' width='32'} + + */ + public static function displaySvgIcon($params) + { + if (empty($params['file'])) { + return ''; + } + + // The path to the icons directory + $icon_path = _PS_THEME_DIR_ . '/_dev/img/icons/' . basename($params['file']); + + // If there aint an image, display a basic error message on dev mode + if (!is_file($icon_path)) { + if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) { + return 'Invalid SVG path: ' . htmlspecialchars($icon_path); + } + return ''; + } + + $svg_content = file_get_contents($icon_path); + + // Build style attribute from parameters + $styles = []; + if (!empty($params['width'])) { + $styles[] = 'width: ' . $params['width'] . (is_numeric($params['width']) ? 'px' : ''); + } + if (!empty($params['height'])) { + $styles[] = 'height: ' . $params['height'] . (is_numeric($params['height']) ? 'px' : ''); + } + if (!empty($params['color'])) { + $styles[] = 'color: ' . $params['color']; + } + + // If we have styles to apply, add them to the SVG + if (!empty($styles)) { + $style_attr = 'style="' . implode('; ', $styles) . '"'; + $svg_content = preg_replace('/(]*?)>/', '$1 ' . $style_attr . '>', $svg_content); + } + + return $svg_content; + } + /* Helper to split the price into euros and cents to make it more stylish + + Usage examples in a TPL file: + {price_split price=$product.price} + + */ + public static function displayPriceSplit($price) + { + //Convert to a string and only keep numbers, commas and dots + $price_string = implode(',', $price); + $price_string = preg_replace('/[^0-9,.]/', '', $price_string); + + // Split by comma and wrap each part in a span + $price_parts = explode(',', $price_string); + if (count($price_parts) >= 2) { + echo '' . $price_parts[0] . ''; + echo '' . $price_parts[1] . ''; + } else { + foreach ($price_parts as $part) { + echo '' . $part . ''; + } + } + } + } diff --git a/is_themecore/src/Hook/Smarty.php b/is_themecore/src/Hook/Smarty.php index 7a9fc00..a83f7ec 100644 --- a/is_themecore/src/Hook/Smarty.php +++ b/is_themecore/src/Hook/Smarty.php @@ -56,5 +56,15 @@ public function hookActionDispatcherBefore(): void } else { return; } + if (!isset($this->context->smarty->registered_plugins['function']['svg_icon'])) { + $this->context->smarty->registerPlugin('function', 'svg_icon', ['Oksydan\Module\IsThemeCore\Core\Smarty\SmartyHelperFunctions', 'displaySvgIcon']); + } else { + return; + } + if (!isset($this->context->smarty->registered_plugins['function']['price_split'])) { + $this->context->smarty->registerPlugin('function', 'price_split', ['Oksydan\Module\IsThemeCore\Core\Smarty\SmartyHelperFunctions', 'displayPriceSplit']); + } else { + return; + } } }