Render order and variable scope with blocks and elements
Permalink
Hi...
I'm having some issues and hoping someone can help me with the rendering order of a page and variable scope. Though I suspect this is more of a scope issue.
I'm producing a package to distribute. It will include functionality that the user will probably want on every single page (but doesn't affect the page layout). As such, I'm producing it as a block that does nothing except include an element. I thought that was the best way to offer the flexibility for the end-user to either use it as a block or include it in the template's header file. Though, it seems like you can add a block directly from the template file... so I'm open to some feedback on this.
Regardless, from this block (or the included element), I'm trying to access variables that the view has access to, specifically $message and $error. I want to display these myself then unset them. Unlike the page can, I can't access them as $message and $error. And it seems I can't use anything like View::getInstance() to get the controller which has these variables.
My test is to include the block on the header of the /profile/edit page. My block has some test code that looks for $message and $error and if it's there, it prints it out, otherwise tells me nothing is there. Even after saving a valid (or invalid) form, my code still tells me that the variables aren't available.
My desired functionality is that the block would print out the $message or $error variable and then unset it so that when the edit page checks for it, it isn't found.
Thanks,
James
I'm having some issues and hoping someone can help me with the rendering order of a page and variable scope. Though I suspect this is more of a scope issue.
I'm producing a package to distribute. It will include functionality that the user will probably want on every single page (but doesn't affect the page layout). As such, I'm producing it as a block that does nothing except include an element. I thought that was the best way to offer the flexibility for the end-user to either use it as a block or include it in the template's header file. Though, it seems like you can add a block directly from the template file... so I'm open to some feedback on this.
Regardless, from this block (or the included element), I'm trying to access variables that the view has access to, specifically $message and $error. I want to display these myself then unset them. Unlike the page can, I can't access them as $message and $error. And it seems I can't use anything like View::getInstance() to get the controller which has these variables.
My test is to include the block on the header of the /profile/edit page. My block has some test code that looks for $message and $error and if it's there, it prints it out, otherwise tells me nothing is there. Even after saving a valid (or invalid) form, my code still tells me that the variables aren't available.
My desired functionality is that the block would print out the $message or $error variable and then unset it so that when the edit page checks for it, it isn't found.
Thanks,
James
I solved the scope problem by putting the variables I wanted to access from the view into $_GLOBALS. Variables set by a block controller are only available to the block's view -- not the page type template (and hence not any elements included in the template). So the globals was the only solutions I could get to work (unfortunately there is no "flash" variable like in Rails where you can send such messages).