Adding a block and setting a template to a page in code

Permalink
Hello everybody. I´m trying to add a block to a page an then set a custom template to it. I managed to add the block to this page (which is a single page) but have no way of setting the template. I´ve tried almost everything, even copying code in process.php. When I log in as an admin I can see the block added to the page, but without Its template applied. This is the current code I´m using:
$p = Page::getCurrentPage();
        $bt_main = BlockType::getByHandle('baibuk_cart');
        $blocks = $p->getBlocks('Header');
        if (count($blocks) == 0) {
            $p->addBlock($bt_main, "Header", array());
            $blocks = $p->getBlocks('Header');
            //$a = Area::get($p, 'Header');
            //$b = Block::getByID($_GET['bID'], $c, $a);
            $b = $blocks[0];
            $permisos = new Permissions($b);
            // we're updating the groups for a particular block
            if ($permisos->canWrite()) {
                $nvc = $p->getVersionToModify();
                $b->loadNewCollection($nvc);
                $data = array("bFileName" => "cart");


Like I said, part of this code is from process.php, executed when you exit edit mode to approve a page version

Thanks in advance, any help would be much appreciated.

Tags:

View Replies: