Viewblock in a Layout

To make things easier, I tried to separate the sidebar into its own file from the main layout in the templates/layout folder. So I decided to use the viewblock.

  • Layout file: aonghas.php
  • Viewblock file: aonghas_sidebar.php

Both files are in templates/layout; in the layout file I defined the following:

<!-- Aonghas Sidebar Menus -->
<?= $this->fetch('aonghas_sidebar'); ?>

And in the viewblock file:

<?php
	$this->start('aonghas_sidebar');
?>
  <!-- Main Sidebar Container -->
  <aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- removed for simplicity -->
  </aside>
  <?php
	$this->end();
?>

However, when I loaded the page/controller that uses it (let’s call it users/index) the sidebar did not appear, but when I moved the sidebar code inside templates/Users/index.php, the sidebar showed up, and I did not intend to have the sidebar code on every view for every controller that I have (since it is repetitive).
I might have overlooked a step but I can’t figure out what it is.

fetch will get whatever is already in the specified block, it won’t do any rendering of templates on its own. Your sidebar template creates that block. What you haven’t shown is the code that’s actually calling the sidebar template.

1 Like