Closed wilco42 closed 11 years ago
I agree that this is a nice feature to have. For now, we will assume that a taglib can export a single object that can be imported as a single variable within a template. In addition, I am thinking that it would be very helpful to give the helper object a reference to the "context" for the current rendering (which might contain the user's locale, for example). To allow this, I am thinking that the object that a taglib exports can be defined as a JavaScript constructor function that takes a single "context" object as the first argument to the constructor. Therefore, the taglib could be similar to the following:
Taglib:
<raptor-tablib>
<uri>i18n</uri>
<helper-object class-name="taglibs/i18n/I18nHelper" />
</raptor-taglib>
Helper Object Definition:
define.Class(
'taglibs/i18n/I18nHelper',
function(require) {
var i18n = require('i18n');
function I18nHelper(context) {
this.context = context;
}
I18nHelper.prototype = {
message: function(key) {
var locale = context.getAttributes().locale;
var localizedMessage = i18n.get(key, locale);
...
}
};
return I18nHelper;
})
Usage in Template:
<c:template
xmlns:c="core"
xmlns:i18n="i18n"
i18n:import-helper-object="i18nHelper">
${i18nHelper.message('HELLO_WORLD')}
</c:template>
What are your thoughts on "import-helper-object" versus "import-object"? I think calling it a "helper object" makes things a little more clear.
I think this is a matter of semantics, but I think import-helper-object is a little more clear as to its purpose and function. I would advocate then that you change "import-functions" to "import-helper-functions" for the same reason.
Feature added. Closing issue.
Currently, you can export helper functions that can be used in any template, but you cannot export a helper object. It may be useful to be able to export an object instead of just a function.
Proposed changes could be something like adding a new tag in the taglib:
Adding a new way to import an object in the:
<c:template
xmlns:c="core"
xmlns:i18n="standard-i18n"
i18n:import-object="i18n"
And then reference it within the template like so: ${i18n.message('HELLO_WORLD')}
Where my i18n object might look like: i18n = { message: function(key) { return key; } };