Commit 4029d273 authored by gijs's avatar gijs

Started on multi-language templates

parent dfd660a7
;
window.W = window.W || {};
(function () {
// Extended version of the _.template function, also supports T{key} to have text translated
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
var templateSettings = {
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g,
translate: /T\{([\s\S]+?)\}/g,
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
var escapeChar = function(match) {
return '\\' + escapes[match];
};
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
// NB: `oldSettings` only exists for backwards compatibility.
template = function(text, settings, oldSettings) {
if (!settings && oldSettings) settings = oldSettings;
settings = _.defaults({}, settings, templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.translate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, translate, evaluate, offset) {
source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
index = offset + match.length;
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
} else if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
} else if (translate) {
source += "'+\n((__t=(W.Translate('" + translate + "')))==null?'':__t)+\n'";
} else if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
// Adobe VMs need the match returned to produce the correct offset.
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + 'return __p;\n';
var render;
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled source as a convenience for precompilation.
var argument = settings.variable || 'obj';
template.source = 'function(' + argument + '){\n' + source + '}';
return template;
};
W.extendedTemplate = template;
})();
\ No newline at end of file
......@@ -33,6 +33,19 @@ window.W = window.W || {};
}
});
W.Translate = window.T = function (k) {
var d = {'key': 'hello world'}
if (k in d) {
return d[k];
} else {
return 'undefined';
}
}
Marionette.TemplateCache.prototype.compileTemplate = function compileTemplate(rawTemplate, options) {
return W.extendedTemplate(rawTemplate, options);
}
var columns = [
{
name: "title",
......
......@@ -127,7 +127,7 @@
<script src="{% static 'playground/vendors/jquery-ui.min.js' %}"></script>
<script src="{% static 'playground/vendors/jquery.mjs.nestedSortable.js' %}"></script>
<script src="{% static 'playground/js/extendedTemplate.js' %}"></script>
<script src="{% static 'playground/js/models.js' %}"></script>
<script src="{% static 'playground/js/collections.js' %}"></script>
<script src="{% static 'playground/js/views.js' %}"></script>
......
......@@ -11,3 +11,8 @@
</ul>
<p>Une partition W est un système ouvert, qui peut tout à fait contenir des morceaux d’autres systèmes notationnels (la partition d’une fugue de Bach, une chorégraphie de Merce Cunningham générée par LifeForms, le texte de <em>Richard III</em>, un plan de scène). Il ne s’agit donc pas de se substituer à un système de notation existant (la danse ou la musique, par exemple, disposent de systèmes notationnels complets et rigoureux) mais de créer une structure multidimensionnelle permettant d’intégrer leurs partitions (et d’autres : texte, conduites lumière, son enregistré...) dans un ensemble plus vaste et articulé.</p>
<%- T('key') %>
T{key}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment