feat(extra-features): Refactor smarty helper
This commit is contained in:
@ -190,25 +190,28 @@ class SmartyHelperFunctions
|
|||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
public static function isBright($hexColor)
|
|
||||||
|
|
||||||
|
/* CUSTOM ADDITIONS BELOW */
|
||||||
|
|
||||||
|
|
||||||
|
public static function isBright($hexColor)
|
||||||
{
|
{
|
||||||
$calculator = new ColorBrightnessCalculator();
|
$calculator = new ColorBrightnessCalculator();
|
||||||
return $calculator->isBright($hexColor);
|
return $calculator->isBright($hexColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper to display SVG icons stored in themes/falcon/_dev/img/icons/
|
public static function displaySvgIcon($params){
|
||||||
|
/*
|
||||||
Registered under public_html/config/smartyfront.config.inc.php
|
Helper to display SVG icons stored in themes/falcon/_dev/img/icons/
|
||||||
|
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
|
||||||
|
|
||||||
Usage examples in a TPL file:
|
Usage examples in a TPL file:
|
||||||
|
|
||||||
{svg_icon file='person.svg' color='red' width='24' height='24'}
|
{svg_icon file='person.svg' fill='red' width='24' height='24'}
|
||||||
{svg_icon file='person.svg' color='#3498db'}
|
{svg_icon file='person.svg'}
|
||||||
{svg_icon file='person.svg' width='32'}
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public static function displaySvgIcon($params)
|
|
||||||
{
|
|
||||||
if (empty($params['file'])) {
|
if (empty($params['file'])) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -226,34 +229,53 @@ class SmartyHelperFunctions
|
|||||||
|
|
||||||
$svg_content = file_get_contents($icon_path);
|
$svg_content = file_get_contents($icon_path);
|
||||||
|
|
||||||
// Build style attribute from parameters
|
// Define SVG attributes that should be replaced/added
|
||||||
$styles = [];
|
$svg_attributes = [
|
||||||
if (!empty($params['width'])) {
|
'width',
|
||||||
$styles[] = 'width: ' . $params['width'] . (is_numeric($params['width']) ? 'px' : '');
|
'height',
|
||||||
}
|
'fill',
|
||||||
if (!empty($params['height'])) {
|
'stroke',
|
||||||
$styles[] = 'height: ' . $params['height'] . (is_numeric($params['height']) ? 'px' : '');
|
'stroke-width',
|
||||||
}
|
'opacity',
|
||||||
if (!empty($params['color'])) {
|
'viewBox',
|
||||||
$styles[] = 'color: ' . $params['color'];
|
];
|
||||||
}
|
|
||||||
|
foreach ($svg_attributes as $attr) {
|
||||||
// If we have styles to apply, add them to the SVG
|
if (!empty($params[$attr])) {
|
||||||
if (!empty($styles)) {
|
$value = $params[$attr];
|
||||||
$style_attr = 'style="' . implode('; ', $styles) . '"';
|
|
||||||
$svg_content = preg_replace('/(<svg[^>]*?)>/', '$1 ' . $style_attr . '>', $svg_content);
|
// Check if attribute already exists in the SVG
|
||||||
}
|
if (preg_match('/' . preg_quote($attr) . '="[^"]*"/', $svg_content)) {
|
||||||
|
// Replace existing attribute
|
||||||
|
$svg_content = preg_replace(
|
||||||
|
'/' . preg_quote($attr) . '="[^"]*"/',
|
||||||
|
$attr . '="' . $value . '"',
|
||||||
|
$svg_content
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Add new attribute to the opening <svg> tag
|
||||||
|
$svg_content = preg_replace(
|
||||||
|
'/(<svg[^>]*?)>/',
|
||||||
|
'$1 ' . $attr . '="' . $value . '">',
|
||||||
|
$svg_content,
|
||||||
|
1 // Only replace the first occurrence
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return $svg_content;
|
return $svg_content;
|
||||||
}
|
}
|
||||||
/* Helper to split the price into euros and cents to make it more stylish
|
|
||||||
|
|
||||||
|
public static function displayPriceSplit($price){
|
||||||
|
/* Helper to split the price into euros and cents to make it more stylish
|
||||||
|
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
|
||||||
|
|
||||||
Usage examples in a TPL file:
|
Usage examples in a TPL file:
|
||||||
{price_split price=$product.price}
|
{price_split price=$product.price}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public static function displayPriceSplit($price)
|
|
||||||
{
|
|
||||||
//Convert to a string and only keep numbers, commas and dots
|
//Convert to a string and only keep numbers, commas and dots
|
||||||
$price_string = implode(',', $price);
|
$price_string = implode(',', $price);
|
||||||
$price_string = preg_replace('/[^0-9,.]/', '', $price_string);
|
$price_string = preg_replace('/[^0-9,.]/', '', $price_string);
|
||||||
@ -269,5 +291,4 @@ class SmartyHelperFunctions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user