oscarotero/view-helpers

Collection of useful functions to use in your templates.

dev-master 2021-12-16 18:05 UTC

This package is auto-updated.

Last update: 2024-12-17 00:21:03 UTC


README

Collection of useful functions to use in your templates.

Text

Collection of helpers for text manipulation:

join

$pieces = ['one', 'two', 'three'];

echo ViewHelpers\Text::join($pieces); // "one, two and three"
echo ViewHelpers\Text::join($pieces, ' | '); // "one | two and three"
echo ViewHelpers\Text::join($pieces, ', ', ' & '); // "one, two & three"

Html

Functions to generate html

element

$element = ViewHelpers\Html::element('div', [
    'id' => 'my-id',
    'class' => ['class1, class2'],
    'style' => [
        'font-size' => '2em',
        'color' => 'blue'
    ],
    'hidden' => true
]);

echo $element; // <div id="my-id" class="class1 class2" style="font-size: 2em; color: blue" hidden>

picture

$element = ViewHelpers\Html::picture(
    [
        'default.jpg',
        '(min-width: 2000px)' => 'image_2000.jpg',
        '(min-width: 1000px)' => 'image_1000.jpg',
        '(min-width: 500px)' => 'image_500.jpg',
    ],
    'Alt text'
);

echo $element;
/*
<picture>
    <source srcset="image_2000.jpg 1x, image_4000.jpg 2x" media="(min-width: 2000px)">
    <source srcset="image_1000.jpg" media="(min-width: 1000px)">
    <source srcset="image_500.jpg" media="(min-width: 500px)">
    <img src="default.jpg" alt="Alt text">
</picture>