Why is on_page_view called for Blocks in all Stacks and Global Areas?
Permalink
I was going through the source code of core concrete5 view.php file in the library. In the render() method I see the following code:
The above code creates an array of all the blocks on the current page and the blocks from all the Global Areas and Stacks in the website. Then later on, there is this code:
The above code loops through all the blocks in the array collected from the previous code and triggers the on_page_view method for them.
My question is: Why should the on_page_view method be called for a Block that is in a Stack or Global Area which is not even called on that page?
Most blocks that have implemented on_page_view are adding some JS/CSS files. If I am making such blocks a part of Stack or Global Area then for every page on my website, those files are loaded even if I don't need them on certain pages.
Is there any solution to this?
Thanks!
$_pageBlocks = $view->getBlocks(); if (!$dsh->inDashboard()) { $_pageBlocksGlobal = $view->getGlobalBlocks(); $_pageBlocks = array_merge($_pageBlocks, $_pageBlocksGlobal); }
The above code creates an array of all the blocks on the current page and the blocks from all the Global Areas and Stacks in the website. Then later on, there is this code:
if (is_array($_pageBlocks)) { foreach($_pageBlocks as $b1) { $b1p = new Permissions($b1); if ($b1p->canRead()) { $btc = $b1->getInstance(); // now we inject any custom template CSS and JavaScript into the header if('Controller' != get_class($btc)){ $btc->outputAutoHeaderItems(); } $btc->runTask('on_page_view', array($view)); } } }
The above code loops through all the blocks in the array collected from the previous code and triggers the on_page_view method for them.
My question is: Why should the on_page_view method be called for a Block that is in a Stack or Global Area which is not even called on that page?
Most blocks that have implemented on_page_view are adding some JS/CSS files. If I am making such blocks a part of Stack or Global Area then for every page on my website, those files are loaded even if I don't need them on certain pages.
Is there any solution to this?
Thanks!
core_scrapbook_display and core_stack_display. And they use it to render their blocks.
Remember that stacks are a special kind of pages...
Correct me if I'm wrong, but scrapbooks are legacy. Stacks replace them.
Only blocks that are part of a stack or a global area are rendered.