A simple tool for porting PHP constant classes to javascript constant objects
Run composer require pamiris/javascript-constants
-- or --
Add the following line to your composer.json:
{
"require": {
"pamiris/javascript-constants": "*"
}
}
To get the executable javascript to define the object, pass the classname and an optional target name into the tool; then include the resulting js inside a script tag on your template:
<?php
//src/myprocject/controller.php
...
use Pamiris\JavascriptConstants\Mapper as ConstantMapper;
...
function someAction()
{
$jsConstants = ConstantMapper::getJavascriptObject('MyNamespace\MyClass');
$this->renderTemplate('mypage',[
...
'scriptContent' => $jsConstants
]);
{# /myproject/page.html.twig #}
{% extends base.html.twig %}
{% block javascripts %}
<script>{{ scriptContent | raw }}</script>
{% endblock %}
Add Twig extension and bundle to auto-configure.
This requires ES2015, which is supported in most broswers. Safari will let you overwrite the objects, Chrome and Firefox will not.
PHP JavascriptConstants is licensed under the MIT License - see the LICENSE file for details.