Jump to content

Getting to the Page associated with the current request


mindplay.dk
 Share

Recommended Posts

I built a basic "block" system, with reusable "blocks" you can add to sidebars, etc. - using just a couple of AsmSelect fields, and it's working great! No code was necessary, just a couple of fields and a few simple templates.

One of those blocks is a menu-block, where the content manager can build a list of pages that should appear in a menu, which you can then add to any page. That's working fine too.

Where I'm stuck, is I'm looping over my list of blocks and rendering them, like so:

 <?php foreach ($blocks as $index => $block): ?>
   <?= $block->render(); ?>
 <?php endforeach; ?>

Very simple - I have different types of blocks that use different templates, and this works nicely.

But I'm having a problem with the menu-block, which needs to know which Page is currently being viewed, so it can add an "active" class to the list-item.

My menu-block template looks something like this:

   <ul class="menu-block-links">
  <?php foreach ($page->pages as $item): ?>
    <li<?= wire('page')->id === $item->id ? ' class="active"' : '' ?>><a href="<?= $item->url ?>"><?= $item->title ?></a></li>
  <?php endforeach; ?>
   </ul>

The problem here, is that wire('page') seems to get overwritten whenever you render() a Page?

In other words, wire('page') gives me the menu-block, because that's what's currently being rendered - but what I need is the Page that was actually requested.

Is there any way to get your hands on that, or is it lost as soon as you call render() on a Page??

Link to comment
Share on other sites

you.try this

<?php foreach ($blocks as $index => $block): ?>
 <?= $block->set('pagamos', $page)->render(); ?>
<?php endforeach; ?>

menu-black tamplate:

<ul class="menu-block-links">
 <?php foreach ($page->pages as $item): ?>
   <li<?= $page->pagamos->id === $item->id ? ' class="active"' : '' ?>><a href="<?= $item->url ?>"><?= $item->title ?></a></li>
 <?php endforeach; ?>
</ul>
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...