Determining the custom template applied to a block from the blocks controller

Permalink 1 user found helpful
How do I check and determine what custom template is applied to a block?

I would like to know because I am making a block with a couple templates, but the view.php always stays the same. So to keep things simple I am running the render function from the controller view method. That way the same view is always rendered, but my CSS for each template needs to target the bID instead of just loading a CSS file. Since the same view is always rendered I need a way to check what template is set on the block and insert my CSS from there.

ob7dev
 
ob7dev replied on at Permalink Best Answer Reply
ob7dev
Within the block controller, additionally to using Concrete\Core\Block\BlockController, you need to add:k
use Concrete\Core\Block\View\BlockViewTemplate;

From your view method get the template:
$b = $this->getBlockObject();
$bvt = new BlockViewTemplate($b);
$template = $bvt->getTemplate();

Returns the full path to the template.
So to determine if a certain block template is active you can search for it:
if (strpos($template, 'templates/my_awesome_template') !== false) {
  // do something as my_awesome_template is active
}