0

我正在使用 CakePHP 1.2。我正在尝试使用 CakePHP 的 jQuery Lazy ( http://jquery.eisbehr.de/lazy/example_basic-usage )。阅读https://book.cakephp.org/1.2/en/The-Manual/Core-Helpers/HTML.html#image上的文档,它显示了如何

<?php echo $html->image('cake_logo.png', array('alt' => 'CakePHP'))?>

产生以下输出:

<img src="/img/cake_logo.png" alt="CakePHP" />

我需要产生这个输出:

<img class="lazy" data-src="/img/cake_logo.png" />

如何$html->image()在 CakePHP 1.2 中使用?问题在于,在语法image(string $path, array $htmlAttributes = array())中,第一个参数是必需的,而在输出中它会产生 的src=...属性img。我需要一个<img... />不包含该src=...属性的输出。我如何在 CakePHP 中使用 $html->image() 来实现这一点?

4

1 回答 1

1

通过内置的 HTML 助手,不修改标签模板是不可能的。因此,如果您需要延迟加载所有图像,那么您可以将image标签模板更改为以下内容:

<img class="lazy" data-src="%s" %s/>

但是,这会与使用该class属性发生冲突。因此,或者您可以扩展 HTML 帮助程序并实现自定义image()方法(或附加方法)。这是一个快速而肮脏的例子,它应该是不言自明的:

function image($path, $options = array())
{
    $defaults = array(
        'class' => null
    );
    $options += $defaults;

    $options['class'] = 'lazy ' . $options['class'];

    $originalTemplate = $this->tags['image'];
    $this->tags['image'] = '<img data-src="%s" %s/>';

    $imageHtml = parent::image($path, $options);

    $this->tags['image'] = $originalTemplate;

    return $imageHtml;
}

也可以看看

于 2018-06-21T19:52:46.360 回答