Adding a block from within a stack into theme (hardcoded)

Permalink 1 user found helpful
Hi all,

I have seen this pop up a few times around the forum but they all seem to be older posts (I apologise if I have missed something).

I have a stack which houses all my default theme blocks that will appear in different areas of my site and I want to be able to pull out a block out of this stack and hardcode it into my theme. Basically meaning this stack is more like a folder containing several different blocks that I can pull from.

Simply, is there a way of calling a block from within a stack and adding it to the theme's view.php without the client having to add these things in?

Any help would be greatly appreciated.

Richard

View Replies:
shadowcomputers replied on at Permalink Reply
shadowcomputers
You can call a stack by using the following:
$stack = Stack::getByName('Test Stack');
$stack->display();


From what I can see there is no way to call one specific block from within the stack, but you could use CSS to hide elements you don't want.
jordanlev replied on at Permalink Reply
jordanlev
Give a name to each block in the stack (click on the block and choose "Custom Template", then enter a name in the text box there).

Then in your page type templates, use this:
<?php Block::getByName('Your Block Name')->display(); ?>


-Jordan
BHWW replied on at Permalink Reply
BHWW
Hi Jordan, what do you do if your block requires header items? i'm trying to include a form, thats help in a stack, but it won't function?

Thanks

Ben
jordanlev replied on at Permalink Reply
jordanlev
I haven't run into this problem myself before (because I haven't needed to put form blocks in stacks before), but I have seen a lot of other people report problems with this. Short of waiting until the bug is fixed (hopefully) in a future release of C5, you might want to figure out what the header items are and manually include them in your theme head.

Not sure if that is a great solution, but it's all I can think of off the top of my head.
mnakalay replied on at Permalink Reply
mnakalay
I think the answer might be in the core code.
if you look in concrete\elements\block_area_add_stack_contents.php you will see how C5 gets a list of all the blocks in a stack
$st = Stack::getByID($_REQUEST['stackID']);
$blocks = $st->getBlocks(STACKS_AREA_NAME);
foreach($blocks as $b) { 
   $bt = $b->getBlockTypeObject();
   $btIcon = $ci->getBlockTypeIconURL($bt);
   $name = $bt->getBlockTypeName();
   if ($b->getBlockName() != '') {
      $name = $b->getBlockName();
   }
   try {
      $bv = new BlockView();
      $bv->render($b, 'scrapbook');
   } catch(Exception $e) {
      print BLOCK_NOT_AVAILABLE_TEXT;
   }

So I suppose if you knew the index of your block within your stack you could just use that instead of the foreach.

I haven't tested it though so just an assumption.
JohntheFish replied on at Permalink Reply
JohntheFish
My guess (untried) is that for the headers you could use similar code to that in the Global Areas or Parent Area addons.
http://www.concrete5.org/marketplace/addons/global-areas/...
http://www.concrete5.org/marketplace/addons/parent-area/...