How do I render a view from within a block action?

Permalink
I can't find this anywhere on the internet. I've got the following code in my block:
public function action_getProductsInCollection()
    {
        $api = $this->getAPI();
        $collectionId = filter_input( \INPUT_GET , 'collectionId' , \FILTER_SANITIZE_NUMBER_INT );
        $products = $api->getProductsByCollectionId( $collectionId );
        include "products_checks_list.php";
    }


I don't want that include statement in there. I've searched the web for a way to display a view from within the block but had no luck. Can anyone help?

 
mnakalay replied on at Permalink Reply
mnakalay
Hello,
If you mean render a view of the block itself, from within the block's controller you can do
$b = $this->getBlockObject();
$bv = new BlockView($b);
$bv->render('view');
fcmjj replied on at Permalink Reply
Tried it, variables I set with $this->set become null
mnakalay replied on at Permalink Reply
mnakalay
$this refers to the current blockview. You are now dealing with a new blockview. If you are trying to set variable for use in view (that's what "set" does) you might want to try
$b->set(...

or maybe
$bv->set(...

I am not 100% sure

If you are trying to set controller's variables values like you would do through the block's settings you might want to try
$b->controller->variableName = xxx;
fcmjj replied on at Permalink Reply
No, those methods don't exist. However I did some research on the code you provided and put this together:

$b = $this->getBlockObject();
$bv = new BlockView($b);
$bv->setViewTemplate( __DIR__ . '/my_template.php');
$bv->renderViewContents(array(
    // my vars here
));


That worked for what I was trying to do. Thanks for your help!