How to call a block from a template ?

Permalink
Hi,

I'm looking to display a block from my template. I don't want to use the Defauts feature to add the block to all my page but code that in the php template file to avoid my customer can modify that...

I know how to call an autonav block but I can't success to include a content block. Any idea ?

Thanks.

dgaussin
 
s2d replied on at Permalink Reply
s2d
I'm pretty sure I saw something on the forum about this earlier, and now searching again for it, since I also need to do this. If I find it, I'll post a link to it here.
s2d replied on at Permalink Reply
s2d
Read this thread and see if it has your solution.

http://www.concrete5.org/index.php?cID=11798...
dgaussin replied on at Permalink Reply
dgaussin
Thanks, but it's not exactly what I'm looking for... I want to call an existing block.
xclydes replied on at Permalink Reply
xclydes
<?php
//Instantiate the block.
$bt_links = BlockType::getByHandle('blockname');
//Set Block values.
$bt_links->parameter = value;
//Use default render action.
$bt_links->render('view');
//OR render using a template.
$bt_links->render('path/templateName');
//OR  use a custom function to manipulate the block for custom display.
foreach ($bt_links as $bt_links){
   some->action;
}
?>


all thats based on info from remos site.
http://www.codeblog.ch/category/concrete5/...

specifically
http://www.codeblog.ch/2009/04/concrete5-blocks-within-templates/...
dgaussin replied on at Permalink Reply
dgaussin
I know how to create a new block like that code but not how to re-use an existant block... like saying.. call the block #48.

I'm trying that, but not exactly that yet...

$b = Block::getByID(48);
   $bv = new BlockView();
   echo  $bv->render($b, 'view');
Remo replied on at Permalink Reply
Remo
Here's some code I wrote a while ago:

$cGlobal = Page::getByPath('/dashboard/scrapbook/global');
$blocks = $cGlobal->getBlocks("Global Scrapbook");
foreach($blocks as $b) {
...
  if ($b->getBlockTypeHandle() == 'music_control') {
    $b->display();
  }
}


It fetches a block with the handle music_control out of the scrapbook. I'm not sure if this is the proper way, but it shows you how to display an already existing block..
Remo replied on at Permalink Reply
Remo
I guess it should be easy as this:

$b = Block::getByID(48); 
$b->display();


but I haven't tested it..
olay replied on at Permalink Reply
olay
anyone any luck or is it me?
dgaussin replied on at Permalink Reply
dgaussin
but I had the error in my phperr.log :

Fatal error: Call to a member function getCollectionID() on a non-object in area.php on line 117

Like here :http://www.concrete5.org/index.php?cID=10634...
ScottC replied on at Permalink Reply
ScottC
where you have your, render, 'view', the 'view' could be 'templates/wonkyview' or something of that sort, any file off of the blockpath at the view depth.
xclydes replied on at Permalink Reply
xclydes
<?php
$newArea = new Area ('NEW');
$newAreaBlocks = $newArea->getAreaBlocksArray($c);
$newBlock = Block::getByID($blockID);
$newAreaBlocks = array_push($newAreaBlocks,$newBlock);
$newArea->display($c, $newAreaBlocks);
?>

I'm not 100% sure but that might be wat u mean. There's a Block::duplicate($nc) command, i'm not 100% sure what it does, but it might come in handy.
Remo replied on at Permalink Reply
Remo
it's duplicates a block instance in the database.

Needed if a new page version is created (each version creates a new block version as well)
xclydes replied on at Permalink Reply
xclydes
wasnt aware of that. thanks for that bit of info.
nerdess replied on at Permalink Reply
nerdess
There seems to be a Concrete5 bug because if I am grabbing a block like you guys described with e.g.

$b = Block::getByID(48);
$bv = new BlockView();
echo  $bv->render($b, 'view');


the issue is that the HTML that got added with TinyMCE gets screwed a bit, see http://www.concrete5.org/community/forums/customizing_c5/confusion-...