This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

brokenpixels2325
brokenpixels2325

I have report widget for the dashboard that I want to use moment.js with. My current solution is just to add moment.js to the bottom of the report widget

<div class="report-widget">
... stuff ...
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js"></script>

This isn't ideal as I'd like to have the file hosted on the server. I'd also prefer to have any scripts load at the bottom or top of the page, but that's not as big an issue.

How can I add a javascript asset and load when my report widget is loaded? Is there a way to inject my <script> tag with those at the top of the page?

brokenpixels2325
brokenpixels2325

Found the solution. Use URL::asset to load an asset from a plugin, use Block:startBlock('head') ... Block:endBlock() to insert things into the head.

<?php Block::startBlock('head') ?>
<script src="<?= URL::asset('plugins/brokenpixels/dashboard/assets/moment.min.js') ?>"></script>
<?php Block::endBlock() ?>

Looking at the source for BlockBuilder, I think that you're meant to use Block::put('head') ... Block::endPut(), but for some reason this causes the entire widget to appear on top of the layout. Seems like a bug, but I might be understanding it wrong.

Last updated

reevader
reevader

Not exactly offtop, so I'll ask here: is there a more efficient way of adding scripts to the head of all pages, other than {%placeholder head%} in the layout and than add manually {% put head %}..js here..{% endput %} to every page? Like inject it once into the main layout and be done with it?

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.