Skip to content

Instantly share code, notes, and snippets.

@zanderwar
Created January 20, 2018 03:18
Show Gist options
  • Save zanderwar/3137b676ac57601c6139945826921ab4 to your computer and use it in GitHub Desktop.
Save zanderwar/3137b676ac57601c6139945826921ab4 to your computer and use it in GitHub Desktop.
Easier requirements for SilverStripe 4
<?php
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Model\SiteTree;
/**
* Class Page
*/
class Page extends SiteTree
{
private static $db = [];
private static $has_one = [];
}
/**
* Class PageController
*/
class PageController extends ContentController
{
private static $allowed_actions = [];
protected function init()
{
parent::init();
$css = [
'/themes/default/bower_components/bootstrap/dist/css/bootstrap.min.css',
];
$js = [
'/themes/default/bower_components/jquery/dist/jquery.min.js',
'/themes/default/bower_components/bootstrap/dist/js/bootstrap.min.js',
];
$afterCss = [];
$afterJs = [];
foreach ($this->getPageRequirements() as $key => $requirements) {
foreach ($requirements as $requirement) {
if (is_array($requirement)) {
list($requirement, $position) = $requirement;
if ($position == 'after') {
$lib = 'after' . ucfirst($key);
${$lib}[] = $requirement;
continue;
}
}
${$key}[] = $requirement;
}
}
$css[] = '/themes/default/css/main.min.css';
$js[] = '/themes/default/js/main.min.js';
$css = array_merge($css, $afterCss);
$js = array_merge($js, $afterJs);
\SilverStripe\View\Requirements::combine_files('common.min.css', $css);
\SilverStripe\View\Requirements::combine_files('common.min.js', $js);
}
public function getPageRequirements()
{
return [
'css' => [],
'js' => []
];
}
}
// Then you can override getPageRequirements in all Page subclasses to inject requirements before or after
// your main.min.js or main.min.css
//
// public function getPageRequirements()
// {
// return [
// 'css' => [
// '/themes/default/js/form-validator.min.js',
// ['/themes/default/js/pages/contact-us.min.js', 'after']
// ],
// 'js' => []
// ];
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment