Using named global blocks from templates

Permalink
As I understand it, from 5.3 onwards we can create a block in the global scrapbook and access it directly from templates!

Superawesome feature (saves db lookups, helps stop clients breaking sites) but how do we use it? What do we throw in the template?

oliwarner
 
oliwarner replied on at Permalink Reply
oliwarner
I've managed to get the block displaying using this:

$b = Block::getByName('header-nav');
$b->display('view', array());


header-nav being the name of my named, global block.

I want to apply a custom template to the block. I'm sure this probably has something to do with the second argument but I've no idea what to pass through.

Any ideas?
oliwarner replied on at Permalink Reply
oliwarner
After stepping through all the render code for blocks, I finally noticed that the "view" part was just including the view.php file, so I thought I'd try wanging in the templates path:

$b = Block::getByName('header-nav');
$b->display('templates/header_nav', array());


And Kazaam! It works. Truly magical.
oliwarner replied on at Permalink Reply
oliwarner
I'm having a problem with one global autonav block.

I have some sidebar navigation. It is supposed to show relevant second level links, with any third-level links if a second/third/etc is active.

I have it set up like this (as a normal block):
Order: sitemap
Viewing Permissions: unckecked
Display Pages: 2nd level
Sub Pages to Display: relevant
Sub-Page Levels: custom (1)

It works as a proper block but not from a global block. It's probably not working because it doesn't know where in the site it is... How do I get that information to the block?
mdodge replied on at Permalink Reply
Has anyone solved this problem? I have a site which has a nav in the header of the page, and another on the side, both of which I would like to be global blocks. Mine is set up as follows:

Order: sitemap
Viewing Permissions: unchecked
Display Pages: Top
Sub Pages to Display: none

I experience the same issue described here when I assign the global template as a "default" through the dashboard "Page Types". But if I edit a page manually and insert the exact same scrapbook block, it works perfectly.

If it helps out at all, I put an echo statement in the controller to show the value of "displayPages" when the nav was being generated. The controller thought the value was 'second_level' even though my global block has it defined as 'top'. I have no clue where this is coming from...none of my nav blocks use 'second_level' anywhere.

As a very temporary workaround, I have a custom template, and if I assign "$controller->displayPages = 'top';" just before the "$controller->generateNav();" call, I can get it to work as expected. This might not be the case if the nav needed to be some level other than 'top'.

Any clues what's going on, or how I can get the global scrapbook block to behave? Any help would be much appreciated!
tbcrowe replied on at Permalink Reply
tbcrowe
I know this thread is old, but I just ran into it myself. You were correct that the block didn't know where in the site it was. This is the solution I came up with.

$b = Block::getByName('autonav'); 
$b->c = $c;
$b->display('view', array());