定制 adhocore/twig-yall 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adhocore/twig-yall

Composer 安装命令:

composer require adhocore/twig-yall

包简介

Lazy load image in twig using malchata/yall.js

关键字:

README 文档

README

It is a twig extension around malchata/yall.js for lazy loading img, picture, video, iframe etc.

(Also supports source tag and srcset attribute).

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

Eager loading of images and videos not only makes webpage slow and clumsy but also consumes a lot of bandwidth.

If you use twig templating system for your view layer, this extension defers loading of resources so they are loaded only when required in viewport.

Installation

composer require adhocore/twig-yall

Usage

First setup twig to register this extension:

// Use your loader of choice
$twig = new Twig\Environment(new Twig\Loader\ArrayLoader);

// Register Yall with defaults
$twig->addExtension(new Ahc\TwigYall\Yall);

// Configuring Yall instance:
$twig->addExtension(new Ahc\TwigYall\Yall(
    'polyfillJs'  => '<custom url to polyfill>',
    'yallJs'      => '<custom url to yall.js>',
    'lazyClass'   => '<default lazy class>',
    'placeholder' => '<default placeholder image url>',
));

Voila, then in twig templates you would either use {% lazyload %} block to lazyload whole block at once OR individually lazyload each resources with {{ lazify() }}.

In both cases, you must call {{ yallify() }} somewhere at the footer.

lazyload

With placeholder config set to 'default.png', below template

<img src="apple.jpg" />                   {# not lazyloaded #}
{% lazyload %}
<img src="ball.jpg" />                    {# lazyloaded #}
<img src="cat.jpg" class="no-lazy" />     {# not lazyloaded #}
<img src="cat.jpg" data-src="..." />      {# not lazyloaded #}
<video poster="vid.jpg">                  {# lazyloaded #}
  <source src="vid1.mp4">                 {# lazyloaded #}
  <source src="vid2.mp4">                 {# lazyloaded #}
</video>
<video class='no-lazy' src="..."></video> {# not lazyloaded #}
<picture><source src="pic.jpg"></picture> {# lazyloaded #}
{% endlazyload %}
<img src="...">                           {# not lazyloaded #}

will be rendered as:

<img src="apple.jpg" />
<img class="lazy yall" src="default.png" data-src="ball.jpg" />
<img src="cat.jpg" class="no-lazy" />
<img src="cat.jpg" data-src="..." />
<video class="lazy yall" poster="default.png" data-poster="vid.jpg">
  <source class="lazy yall" data-src="vid1.mp4">
  <source class="lazy yall" data-src="vid2.mp4">
</video>
<video class='no-lazy' src="..."></video>
<picture><source class="lazy yall" data-src="pic.jpg"></picture>
<img src="...">

lazify

only src

<img {{ lazify("/my/img.jpg") }} />

will be rendered as:

<img class="lazy yall" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=" data-src="/my/img.jpg" />

See stackoverflow for the usage of data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=.

with class

<img {{ lazify("logo.png", "cls1 cls2") }} />

will be rendered as:

<img class="cls1 cls2 lazy yall" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=" data-src="logo.png" />

custom placeholder

<img {{ lazify("logo.png", "cls1 cls2", "/img/placeholder.png") }} />

will be rendered as:

<img class="cls1 cls2 lazy yall" src="/img/placeholder.png" data-src="logo.png" />

video poster

<video {{ lazify({poster: "video/poster.jpg"}, "video", "dummyposter.jpg") }}>

will be rendered as:

<video class="video lazy yall" poster="dummyposter.jpg" data-poster="video/poster.jpg">

source tag

<source {{ lazify({srcset: "img2x.jpg 2x, img1x.jpg 1x"}) }}>

will be rendered as:

<source class="lazy yall" data-srcset="img2x.jpg 2x, img1x.jpg 1x">

src+srcset

<img {{ lazify(["src.jpg", "src2x.jpg 2x", "src1x.jpg 1x"], "", "dummy.jpg") }} />

will be rendered as:

<img class="lazy yall" src="dummy.jpg" data-src="src.jpg" data-srcset="src2x.jpg 2x, src1x.jpg 1x" />

yallify

Important: Do not forget to put the yall loader somewhere in the footer twig template:

{{ yallify() }}

Which by default loads yall 3.1.7 with polyfills. You can set yall.js version, and turn off polyfill like so:

{{ yallify("3.1.6", "") }} {# load yall v3.1.6 but not polyfill #}

You can pass yall options in third param. For event callbacks wrap it in <raw></raw>:

{{ yallify(null, null, {observeChanges: true, events: {load: "<raw>function(){}</raw>"}}) }}

will be rendered as:

<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver" async></script>
<script src="https://unpkg.com/yall-js@3.1.7/dist/yall.min.js" async></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
  window.setTimeout(function () {
    yall({
      "observeChanges": true,
      "events": {
        "load": function(){}
      },
      "lazyClass": "lazy"
    });
  }, 99);
});
</script>

PS: The inputs sent to lazify() or yallify() are not validated by this library.

From malchata/yall.js:

Use appropriate width and height attributes, styles, and lightweight placeholders for your images.

Contributing

Please check the guide

LICENSE

© MIT | 2019, Jitendra Adhikari

adhocore/twig-yall 适用场景与选型建议

adhocore/twig-yall 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.93k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2019 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「twig-yall」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 adhocore/twig-yall 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adhocore/twig-yall 我们能提供哪些服务?
定制开发 / 二次开发

基于 adhocore/twig-yall 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-15