MVC/templates, replace javascript_include_when_exists() for more universal theme_file_or_default() template function.

The legacy code will still use a plain file_exists() for the optional theme.js, which keeps both in a working state after this commit.
To be able to move more files into the template directories for https://github.com/opnsense/core/pull/2124 we need to keep existing plugins functional, so we could integrate these changes into a minor release.
This commit is contained in:
Ad Schellevis 2018-01-27 12:23:43 +01:00
parent 201c62d60c
commit cbcb9da2ee
3 changed files with 23 additions and 7 deletions

View File

@ -9,6 +9,26 @@ use Phalcon\Session\Adapter\Files as SessionAdapter;
use OPNsense\Core\Config;
use OPNsense\Core\Routing;
/**
* search for a themed filename or return distribution standard
* @param string $url relative url
* @param array $theme theme name
* @return string
*/
function view_fetch_themed_filename($url, $theme) {
$search_pattern = array(
"/themes/{$theme}/build/",
"/"
);
foreach ($search_pattern as $pattern) {
$filename = "/usr/local/opnsense/www{$pattern}{$url}";
if (file_exists($filename)) {
return str_replace("//", "/", "/ui{$pattern}{$url}");
}
}
return $url; // not found, return source
}
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
@ -43,11 +63,7 @@ $di->set('view', function () use ($config) {
'compiledSeparator' => '_'
));
// register additional volt template functions
$volt->getCompiler()->addFunction('javascript_include_when_exists', function ($local_url) {
$chk_path = "str_replace('/ui/','/usr/local/opnsense/www/',".$local_url.")";
$js_tag = "'<script src=\"'.$local_url.'\"></script>'";
return "file_exists(".$chk_path.") ? ".$js_tag." :''";
});
$volt->getCompiler()->addFunction('theme_file_or_default', view_fetch_themed_filename);
return $volt;
},

View File

@ -236,8 +236,7 @@
<script src="/ui/js/opnsense.js"></script>
<script src="/ui/js/opnsense_ui.js"></script>
<script src="/ui/js/opnsense_bootgrid_plugin.js"></script>
{{javascript_include_when_exists('/ui/themes/' ~ theme_name ~ '/build/js/theme.js')}}
<script src="{{theme_file_or_default('/js/theme.js', theme_name)}}"></script>
</head>
<body>
<header class="page-head">

View File

@ -0,0 +1 @@
/* Empty placeholder for theme javascript override */