Jump to content

Access $page inside render field


Tom.
 Share

Recommended Posts

I'm rendering out a foreach based on a Repeater field, however I'm using it for link blocks and I don't want it to display if the block links to the current page. 

Using $item->link != $page doesn't work because $page returns the page the repeater is on, not the page we are currently viewing. 

 

EDIT: Explained 'betterer' 

Sorry when I say render out a foreach, I quite literally mean $page->render("repeater_block"); inside /fields/repeater_block.php using $page will return the page the repeater is on. 

So doing $page->get(1023)->render("repeater_block"); means I can't access the current page inside the repeater. Even when using $page. 

Link to comment
Share on other sites

7 hours ago, tpr said:

Do they? Aren't you overriding $page somewhere in your code?

 

7 hours ago, LostKobrakai said:

If you find $page to not return the correct page (overwritten somewhere or whatever else) just use wire('page') or $this->wire('page') (depending on the context) instead.

Sorry when I say render out a foreach, I quite literally mean $page->render("repeater_block"); inside /fields/repeater_block.php using $page will return the page the repeater is on. 

So doing $page->get(1023)->render("repeater_block"); means I can't access the current page inside the repeater. Even when using $page. 

7 hours ago, Macrura said:

don't you need to use $item->getForPage()  (should return the page the $item is assigned to) <?> at least that's how i always deal with repeaters if they are not in $page context

Thanks for your input, I've explained more above - @Macrura @LostKobrakai @tpr

Link to comment
Share on other sites

You use wire("page")

If rendering a repeater field from another page, let's say

echo $pages->get(1001)->render("repeater_field");

So in template/fields/repeater_field.php

echo "<p>Page containing the repeater: " . $page->title . " " . $page->id . "</p>";
echo "<p>Page currently requested: " . wire("page")->title . " " . wire("page")->id . "</p>";

// render each repeater 
echo $value->each("<section><h2>{title}</h2>{body}</section>");

 

  • Like 2
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...