Add dynamic JS and non-JS/CSS content to HTML-head with Block
PermalinkFor these to work correctly I need
1. to include open-graph meta tags in the HTML-head.
2. to include a generated java script element in the HTML-head.
I already have code that generates the meta tags/java script. I used it in an C5 elements that were included in the header of the theme in my C5 installation. But now I want this to get an installable Block that can be distributed to other installations and automatically adds the required information.
I have seen that I can add JS and CSS files to the head with magic files/folders or addHeaderItem. But how can I add other elements/generated java script elements to the head?
The code that generates the meta-tags and the required java script element is given in the attached files for reference.
Regards, Tilmann
http://www.php.net/manual/en/language.types.string.php#language.typ...
In a block controller you can also use the controller event function on_page_view() instead of on_start().
I seem to remember reading that it was a more efficient place to add header items in block controllers, but can't find where and don't know whether it is significant. So maybe someone else can verify or correct me on that.
on_page_view() is described as "the only way to inject items into the page's header from within a block" in the official documentation.
http://www.concrete5.org/documentation/developers/blocks/mvc-approa...
Thanks @JohntheFish for correcting me. I will update my answer.
ps. I think it would only be fair that you mark his answer as the correct one as opposed to mine as my answer was not correct until he corrected it.
Regarding how to add content to the header I think the documentation of
$controller->addHeaderItem($file)
This is what I added to controller.php (in the path /concrete/controllers/dashboard):
public function on_page_view() { $this->addHeaderItem('<script src="https://images-na.ssl-images-amazon.com/images/G/01/cba/js/jquery.js" type="text/javascript"></script> <script src="https://images-na.ssl-images-amazon.com/images/G/01/cba/js/common/cba_shared.js" type="text/javascript"></script> <script src="https://images-na.ssl-images-amazon.com/images/G/01/cba/js/shoppingcart/merchant_cart.js" type="text/javascript"></script>'); }
But it didn't seem to add anything to the header when I view the page source.
You can use the on_page_view() method of BlockController with addHeaderItem() to add any content to the header, not just js and css file links.
EDIT: You can only put strings in there. So you can either modify it to use output buffering (ob_start) or just put it in a big string. But either way those functions will need to return a string.
Like you can just do