我使用的是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" />
我如何在CakePHP 1.2中使用$html->image()
做到这一点?问题在于,在语法image(string $path, array $htmlAttributes = array())
中,第一个参数是必需的,并且在输出中它会生成img
的src=...
属性。我需要一个不包含src=...
属性的<img... />
输出。如何在CakePHP中使用$html->image()来实现这一点?
发布于 2018-06-22 03:52:46
通过内置的HTML助手,不修改标记模板是不可能的。因此,如果您需要在所有图像上进行延迟加载,那么您可以例如将image
标记模板更改为以下内容:
<img class="lazy" data-src="%s" %s/>
但是,这将与使用class
属性发生冲突。因此,您也可以扩展HTML helper并实现一个自定义的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;
}
另请参阅
https://stackoverflow.com/questions/50975465
复制相似问题