feat(extra-features): Add svg icon and price splitter smarty functions

These are just helpers and useful for many prestashop designs
This commit is contained in:
2025-11-20 09:53:31 +01:00
parent abd8d60502
commit 51fcda227a
2 changed files with 85 additions and 0 deletions

View File

@ -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('/(<svg[^>]*?)>/', '$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 '<span class="euros" aria-label="euros">' . $price_parts[0] . '</span>';
echo '<sup class="cents" aria-label="cents">' . $price_parts[1] . '</sup>';
} else {
foreach ($price_parts as $part) {
echo '<span>' . $part . '</span>';
}
}
}
}

View File

@ -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;
}
}
}