【问题标题】:cakephp element parameters with html带有html的cakephp元素参数
【发布时间】:2014-10-15 08:03:58
【问题描述】:

我想渲染一个包含静态文本的 cakephp 元素。该元素应该包含帮助、文本、图像等,但它应该只显示与当前正在呈现的页面相关的内容。

我想到了两种方法,其中一种是简单地将html内容作为参数从视图中传递如下:

echo $this->element('help-bar-content', array(
    'title' => 'Element title',
    'help_text'=> '<div>
            A lot of text and tags go here.
            <br/>
            <img class="img-class" src="/img/location"/>
            </div>'
        ));

但它很难看,因为它会将 html 混合到 html 中的 php 中。第二种方法是有很多元素,每个元素都对应一个help_text参数,并按如下方式使用它们:

echo $this->element('help-bar-content', array(
    'title' => 'Element title',
    'help_text'=> element('help_element1')));

我认为哪个更好,但是我会有很多 LOT 不可重用的元素,因为我有很多观点。

是否有第三种(更好的)方法可以做到这一点?

【问题讨论】:

    标签: php html cakephp code-reuse reusability


    【解决方案1】:

    将所有帮助文本放在一个switch语句或多个ifs中在元素内​​m>,并给每个帮助文本一个字符串作为标识符,并将该字符串传递给元素。然后根据该字符串有条件地显示文本。

    <?php if ($helpText === 'foo') : ?>
    <p>foo</p>
    <?php endif; ?>
    

    注意 $helpText follows the convention 而 $help_text 没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多