使用 CakePHP 3 在视图中查看

View inside a view using CakePHP 3

是否可以在另一个视图中显示一个视图?

我有以下代码:

<?php if ($result->type === 'brochure') : ?>
    <div>
        // massive template block
    </div>
<?php elseif ($result->type === 'library') : ?>
    <div>
        // massive template block different from above
    </div>
<?php else : ?>
    <div>
        // massive template block different from both above
    </div>
<?php endif; ?>

可以这么说,我想用内容块替换那些。我查看了 view blocks,但我要么用错了,要么没有按照我的意愿去做。

这在 CakePHP 3 中可行吗?

你可以为此使用元素。

首先,您应该在 src/Template/Element 目录中创建 .ctp 格式的元素,如下所示

// in brochure.ctp file in  src/Template/Element
 <div>
    // your massive template block
</div>

然后你可以这样调用元素:

<?php if ($result->type === 'brochure') : ?>

      <?= $this->element("brochure") ?>

<?php elseif ($result->type === 'library') : ?>

      <?= $this->element("library") ?>

<?php else : ?>

      <?= $this->element("default") ?>

<?php endif; ?>