A SilverStripe Module with template methods to quickly make use of FocusPoint, LazySizes, and Object-fit

Overview

LazyFocusFit

A SilverStripe module with template methods to quickly make use of FocusPoint, LazySizes and object-fit.

Requirements

PHP

JS/CSS

Installation

composer require evanshunt/lazy-focus-fit

After installing this module, ensure front-end requirements are installed and properly initiated within your project.

Usage

This module adds 3 primary methods to SilverStripe Image objects, to allow quick creation of responsive image markup eather as <img> or <picture> tags or attributes which can be applied to any element for use as a background image.

Additional methods allow configuring the images to use object-fit, crop according to a specific aspect ratio, or add additional html attributes.

ResponsiveImg()

This method allows generating an <img> tag with a number of possible image sizes. On page load a 32px wide image will be loaded and the correct size will be lazyloaded using LazySizes auto-sizes functionality. A future version may allow explicitly defining a sizes attribute or using the browser's built in responsive sizing w/o LazySizes.

Example

$Image.ResponsiveImg(classname, 1200, 800, 600)

ResponsivePicture()

This method generateds a <picture> element, and allows for more explicit art direction than ResponsiveImg(). You can define media query conditions under which your images will be shown. After the first classname argument, subsequent arguments should take the following format:

{ImageWidth}-{WidthDescriptor}-{PixelDensityDescriptor} {MinWidth}

Width and Pixel density descriptors are optional as defined here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source#Attributes

MinWidth should be a screen width for a (min-width: xxx) media query, or 'default'. Start with largest width and work your way down to default.

Examples

$Image.ResponsivePicture(classname, 770-1x 1440-2x 992px, 496-1x 992-2x default)
$Image.ResponsivePicture(classname, 770-770w 992px, 496-496w default)
$Image.ResponsivePicture(classname, 770 992px, 496 default)

ResponsiveBgAttributes

This method generates html attributes, not an entire element, it is used to apply a background image. The arguments operate the same way as ResponsivePicture()

Be aware that this method generates class and style attributes so adding these to your template code will result in dublicated attributes.

Example

<div $Image.ResponsiveBgAttributes(classname, 770-1x 1440-2x 992px, 496-1x 992-2x default)>
    ...
</div>

Helper methods

AddAttribute()

Adds additional attribute to the <picture> element generated by ResponsivePicture() or the <img> attribute generated by ResponsiveImg(). Must be called before the markup generating method.

Example

$Image.AddAttribute(aria-hidden, true).ResponsivePicture(classname, 770 992px, 496 default). 

AddImgAttribute()

Adds additoinal attribute to the <img> element w/in the <picture> generated by ResponsivePicture(). Must be called before the markup generating method.

Example

$Image.AddImgAttribute(data-foo, bar).ResponsivePicture(classname, 770 992px, 496 default)

AddAspectRatio()

Crops image to a specific proportion, centered on the FocusPoint, for use with ResponsivePicture() and ResponsiveImg(). Must be called before the markup generating method.

Example

$Image.AddAspectRatio(6, 9).ResponsivePicture(classname, 770 992px, 496 default)

ObjectFit()

Adds object-fit styles to style tag of ResponsivePicture() and ResponsiveImg(), also adds necessary data-attributes to work with objectFitPolyfill. Object position values come from FocusPoint.

By default object-fit: cover; is applied, but alternative values (fill, contain, scale-down) can be passed as an argument.

Examples

$Image.ObjectFit().ResponsivePicture(classname, 770 992px, 496 default)
$Image.ObjectFit(contain).ResponsivePicture(classname, 770 992px, 496 default)
You might also like...
Foil brings all the flexibility and power of modern template engines to native PHP templates

Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simple, clean and concise templates with nothing more than PHP.

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.

Smarty 3 template engine smarty.net Documentation For documentation see www.smarty.net/docs/en/ Requirements Smarty can be run with PHP 5.2 to PHP 7.4

Native PHP template system

Plates Plates is a native PHP template system that's fast, easy to use and easy to extend. It's inspired by the excellent Twig template engine and str

View template engine of PHP extracted from Laravel

Blade 【简体中文】 This is a view templating engine which is extracted from Laravel. It's independent without relying on Laravel's Container or any others.

PHP template engine for native PHP templates

FOIL PHP template engine, for PHP templates. Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simpl

A lightweight template parser used by PyroCMS.

Lex Lex is a lightweight template parser. Lex is released under the MIT License and is Copyrighted 2011 - 2014 PyroCMS Team. Change Log 2.3.2 Convert

Laravel package template

REPLACE Simple and flexible package template. Usage Replace all occurances of REPLACE (case sensitive) with the name of the package namespace. E.g. th

The Templating component provides all the tools needed to build any kind of template system.

Templating Component The Templating component provides all the tools needed to build any kind of template system. It provides an infrastructure to loa

Provides a GitHub repository template for a PHP package, using GitHub actions.

php-package-template Installation 💡 This is a great place for showing how to install the package, see below: Run $ composer require ergebnis/php-pack

Releases(v1.1.0)
Owner
Evans Hunt
We make cool stuff.
Evans Hunt
PHP Template Attribute Language — template engine for XSS-proof well-formed XHTML and HTML5 pages

PHPTAL - Template Attribute Language for PHP Requirements If you want to use the builtin internationalisation system (I18N), the php-gettext extension

PHPTAL 175 Dec 13, 2022
A template abstraction prototype for PHP template engines

Schranz Templating A template abstraction prototype for PHP template engines. This project should help to find a way for a general Template Render Int

Schranz Templating 16 Dec 7, 2022
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

Ilian 6 Jul 22, 2022
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

Ilian 6 Jul 22, 2022
A ready-to-use Model View Controller template in PHP

PHP-MVC-Template A ready-to-use Model View Controller template in PHP Use this repo as a template! (Or clone it) Start to configure your MVC file Afte

Loule | Louis 20 Dec 26, 2022
Twig, the flexible, fast, and secure template language for PHP

Twig, the flexible, fast, and secure template language for PHP Twig is a template language for PHP, released under the new BSD license (code and docum

Twig 7.7k Jan 1, 2023
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.

Latte: amazing template engine for PHP Introduction Latte is a template engine for PHP which eases your work and ensures the output is protected again

Nette Foundation 898 Dec 25, 2022
A complete and fully-functional implementation of the Jade template language for PHP

Tale Jade for PHP Finally a fully-functional, complete and clean port of the Jade language to PHP — Abraham Lincoln The Tale Jade Template Engine brin

Talesoft 91 Dec 27, 2022
⚡️ Simple and fastly template engine for PHP

EasyTpl ⚡️ Simple and fastly template engine for PHP Features It's simple, lightweight and fastly. No learning costs, syntax like PHP template It is s

PHPPkg 19 Dec 9, 2022
A PHP project template with PHP 8.1, Laminas Framework and Doctrine

A PHP project template with PHP 8.1, Laminas Framework and Doctrine

Henrik Thesing 3 Mar 8, 2022