jaws-project / jaws

Jaws is a Web Application Framework and Content Management System for building dynamic web sites.
htt://jaws-project.com
39 stars 17 forks source link

Improving Jaws_Template #9

Open afz opened 11 years ago

afz commented 11 years ago
mkhahani commented 11 years ago

Currently we define JavaScript global variables in html templates and set them by php setVariable() function. So the developer has to set them twice(html and php) and when changing, thrice(html, php, js). Moreover we may have many variables that make the template dirty.

I think we should set them in a single variable for example CONST that is an array of those variables. A simple implementation may looks like this:

PHP:

$const = array();
$const['variable_x'] = _t('X_VARIABLE');
$const['variable_y'] = _t('Y_VARIABLE');
..
$tpl->SetVariable('CONST', json_encode($const));

Template:

var CONST = eval("({CONST})");

Javascript:

alert(CONST.variable_x);

So we have just single line of code in our template and can manage variables easily. An enhancement is to move them to the core and define variables by an API like this:

$GLOBALS['js']->AddVariable('variable_x', _t('X_VARIABLE'));

and no codes in template!