Jump to content

Smart Widgets / Blocks


Lars
 Share

Recommended Posts

Hi,

I am building a first landing page that is basically built up by blocks of other pages. Something like the widgets in the Blog module.

I load the blocks I want by using a Page type field and select and re-arrange the order of the blocks to be shown on the first page.

Each block then being a page of it's own it has awareness of itself only. For example I could make an image gallery block that shows the images I uploaded to that specific block. So far no problem.

My issue is, how can I make the block (i.e. page) aware of what is going on in the page it is loaded into? Making this a fixed thing is ok, I can have the "Home Page" load and render my "Block A" page. The template for "Block A" can then read stuff from "Home Page" and perform things based on information in "Home Page".

Now the issue: If I want to be able to load this "Block A" page into say "Products" page as well as "Home Page" then the template for "Block A" would need to know who called it so that it can look into what is available in the calling page.

I was thinking for my "Home Page" and my "Product Page" to set a flag as a Session Variable. Going by Page Name would also work but have to be hard coded as well as able to deal with potential URL segments.

I just feel I am missing something, that there is another and more proper way for "Block A" to know what is in fact the primary page being loaded and that it is a part of being rendered into

Thoughts anyone?

Link to comment
Share on other sites

Something like this will work:

<?php

/**
 * On a page that loads the widgets
 *
 */

$widgets = $pages->find("parent=/widgets/");

foreach($widgets as $widget) {			
    $widget->set('motherPage', $page);
    echo "<div class='widget {$widget->template->name}'>";
    echo $widget->render();
    echo "</div>";
}
<?php

/**
 * logic on a widget page
 *
 */

// The $page variable of the mother Page bound to $motherPage.
$motherPage = $page->motherPage; 

// Throw 404 if a guest access the widget directly 
if(!$motherPage && !$user->isLoggedin()) throw new Wire404Exception();

echo $page->body;
  • Like 5
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...