feat(extra-features): Refactor smarty helper
This commit is contained in:
@ -192,32 +192,26 @@ class SmartyHelperFunctions
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* CUSTOM ADDITIONS BELOW */
|
/* CUSTOM ADDITIONS BELOW */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static function isBright($hexColor)
|
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){
|
||||||
|
/*
|
||||||
|
Helper to display SVG icons stored in themes/falcon/_dev/img/icons/
|
||||||
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
|
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 '';
|
||||||
}
|
}
|
||||||
@ -235,36 +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'];
|
];
|
||||||
}
|
|
||||||
|
|
||||||
// If we have styles to apply, add them to the SVG
|
foreach ($svg_attributes as $attr) {
|
||||||
if (!empty($styles)) {
|
if (!empty($params[$attr])) {
|
||||||
$style_attr = 'style="' . implode('; ', $styles) . '"';
|
$value = $params[$attr];
|
||||||
$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
|
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);
|
||||||
@ -280,64 +291,4 @@ class SmartyHelperFunctions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Helper to add a read more function to text
|
|
||||||
|
|
||||||
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
|
|
||||||
|
|
||||||
Use this JS in public_html/themes/falcon/_dev/js/theme/custom.js to handle the read more link:
|
|
||||||
|
|
||||||
// Read more buttons
|
|
||||||
$(".read-more-text").on("click", function (event) {
|
|
||||||
event.preventDefault();
|
|
||||||
const $link = $(this);
|
|
||||||
const $container = $link.closest(".read-more");
|
|
||||||
$container.find(".truncated-after").toggleClass("d-none");
|
|
||||||
$container.find(".ellipsis").toggleClass("d-none");
|
|
||||||
$link.find(".read-more").toggleClass("d-none");
|
|
||||||
$link.find(".read-less").toggleClass("d-none");
|
|
||||||
});
|
|
||||||
|
|
||||||
Usage example in a TPL file:
|
|
||||||
{read_more text=$product.description_short maxLength=10 readMoreText={l s='Read more' d='Shop.Theme.Catalog'} readLessText={l s='Read less' d='Shop.Theme.Catalog'}}
|
|
||||||
|
|
||||||
*/
|
|
||||||
public static function readMore($params){
|
|
||||||
if (empty($params['text'])) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$text = $params['text'];
|
|
||||||
$maxLength = isset($params['maxLength']) ? (int)$params['maxLength'] : 200;
|
|
||||||
$readMoreText = isset($params['readMoreText']) ? $params['readMoreText'] : 'Read more';
|
|
||||||
$readLessText = isset($params['readLessText']) ? $params['readLessText'] : 'Read less';
|
|
||||||
|
|
||||||
// Strip and count text length
|
|
||||||
$text_stripped = strip_tags($text);
|
|
||||||
|
|
||||||
if (strlen($text_stripped) <= $maxLength) {
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split text and add read more link
|
|
||||||
$truncatedBefore = substr($text_stripped, 0, $maxLength);
|
|
||||||
|
|
||||||
$lastSpace = strrpos($truncatedBefore, ' ');
|
|
||||||
if ($lastSpace !== false) {
|
|
||||||
$truncatedBefore = substr($truncatedBefore, 0, $lastSpace);
|
|
||||||
$truncatedAfter = substr($text_stripped, $lastSpace);
|
|
||||||
} else {
|
|
||||||
$truncatedAfter = substr($text_stripped, $maxLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
'<div class="read-more">'.
|
|
||||||
'<span class="truncated-before">' . $truncatedBefore . '</span>' .
|
|
||||||
'<span class="ellipsis">...</span>' .
|
|
||||||
'<span class="truncated-after d-none">' . $truncatedAfter . '</span>' .
|
|
||||||
'<a class="read-more-text d-flex" href="#">' .
|
|
||||||
'<span class="read-more">' . $readMoreText . '</span>' .
|
|
||||||
'<span class="read-less d-none">' . $readLessText . '</span>' .
|
|
||||||
'</a>'.
|
|
||||||
'</div>';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user