1

Is it possible to display a view inside another view?

I have the following code:

<?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; ?>

I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.

Is this possible in CakePHP 3?

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69

1 Answers1

3

you can use elements for that.

first you should create elements in src/Template/Element directory with .ctp format like this

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

then you can call elements like this :

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

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

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

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

<?php else : ?>

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

<?php endif; ?>
Hamidreza
  • 694
  • 6
  • 19
  • 1
    It should be "**T**emplate" and "**E**lement", the lowercase names won't work with case sensitive filesystems. – ndm Nov 11 '17 at 14:36