JS and CSS addHeaderItem

Permalink Browser Info Environment
Hi,

Thank you for this great block!

I'm trying to have a block with a JS file and CSS but only embed ONCE so I tried in the view.php to add this code on the view.php

$html = Loader::helper('html');
$bv = new BlockView();
$bv->setBlockObject($this->getBlockObject());
$this->addHeaderItem($html->javascript($bv->getBlockURL() . '/javascript.js'));

but unfortunatelly it's not working (I know that normally this should go in the controller.php... )

Do you have a solution for this ?
This is maybe more a general question but if I put my code in the view.js will it be integrated only once or for each block (for exemple 2 time if I have two same block in the same page) ?

Thanks for your help

Mikhaél

Type: Discussion
Status: New
appliculture
View Replies:
theblockery replied on at Permalink Reply
theblockery
Hi Mikhaél,
There are a few different solutions to this. The simplest solution is to create 2 new directories in your block folder (next to the existing view.php file): one called "js" and one called "css". Put your javascript file in the "js" folder, and your css file in the "css" folder. Anything inside those folders will automatically be included by Concrete5 (this is a standard functionality of Concrete5, and is not specific to Designer Content Pro). Note that C5 will only notice files at the top level of those directories, so if you create a sub-directory inside "js" or "css", concrete5 will not load those files... only files directly inside "js" and "css".

If you require more control over how the files are loaded (for example, they are in a different location, or you need to dictate the order in which files are loaded), you can actually load them from your view.php file via javascript, using the ccm_addHeaderItem function, like so:
<script>
ccm_addHeaderItem('<?php echo $this->getBlockURL(); ?>/your-css-file.css', 'CSS');
ccm_addHeaderItem('<?php echo $this->getBlockURL(); ?>/your-js-file.js', 'JAVASCRIPT');
</script>

This too is standard Concrete5 functionality (not specific to Designer Content Pro).


I hope one of these two methods is helpful to you. If not, let me know what the exact problem is and I can try to figure out another solution for you.

Thanks,
Jordan

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.