Blazy for hard-coded image, non-media entity

Created on 13 May 2024, about 1 month ago
Updated 28 May 2024, 19 days ago

Problem/Motivation

What is the recommended way to use blazy for non-entity media? For example, if we are already using blazy for media entities, but we also have an image hard-coded into a theme template. Is there a way to invoke blazy for this image?

💬 Support request
Status

Fixed

Version

3.0

Component

Miscellaneous

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jgjg
  • 🇮🇩Indonesia gausarts

    > image hard-coded into a theme
    If you could place your code, I might be able to help better?

    However, hard-coded or not, Blazy works perfectly fine with dead or archived HTML, twig, and PHP. It started with dead HTML, anyway.
    To get a better idea on how Blazy works, try viewing the existing Blazy formatter output, CTRL/CMD + U.

    The following should give you a big picture in TWIG usages.

    See blazy.libraries.yml for more advanced usages:
    {{ attach_library('blazy/load') }}
    {{ attach_library('blazy/ratio') }}
    
    The SVG placeholder is better than gif, but just longer and uglier:
    {% set placeholder = "data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%201%201'%2F%3E" %}
    
    {% set url = 'https://www.drupal.org/files/One.gif' %}
    {%
      set attributes = create_attribute({
        'data-src': url,
        'src': placeholder,
        'loading': 'lazy',
        'class': ['media__element', 'b-lazy'],
        'width': 640,
        'height': 360,
        'alt': 'Preview',
      })
    %}
    
    For more aspect ratio, see <em>css/components/blazy.ratio.css</em>
    <div class="media media--ratio media--ratio--169">
      <img{{ attributes }}/>
    </div>
    

    Or the simplest, but needs more theming to avoid collapsed images, etc:

    {{ attach_library('blazy/load') }}
    <img data-src="{{ url }}" src="{{ placeholder }}" alt="{{ 'Preview'|t }}" class="b-lazy" loading="lazy" width="640" height="360" />
    

    placeholder can be:

    '/blank.svg'
    '/blank.gif'
    'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
    "data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%201%201'%2F%3E"

    The first two hit your server with HTTP requests, bad but can be handy from within Views results as noted at project home.
    The last (data URI) won't, normal Blazy usages.
    For dynamic/ field image urls, use core function here https://www.drupal.org/node/2486991#s-file-urluri → , or twig_tweak module for more functionality.

    Pro tip:
    Don't be fooled by Console Inspector (F12), use its Network tabs to verify the lazy load workflow.

  • Status changed to Fixed about 1 month ago
  • 🇮🇩Indonesia gausarts

    Updated attributes to img_attributes in case copied as is and conflicting with normal theme reserved attributes.

    Feel free to share your findings.

    Thank you.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024