6 var compiledTemplates
= {},
11 * Register a new compiler.
13 * A compiler is any object that implements a compile() method. The compile() method must
14 * return a Template interface with a method render() that returns HTML.
16 * The compiler name must correspond with the name suffix of templates that use this compiler.
18 * @param {string} name Compiler name
19 * @param {Object} compiler
21 registerCompiler: function ( name
, compiler
) {
22 if ( !compiler
.compile
) {
23 throw new Error( 'Compiler must implement a compile method' );
25 compilers
[ name
] = compiler
;
29 * Get the name of the associated compiler based on a template name.
31 * @param {string} templateName Name of a template (including suffix)
32 * @return {string} Name of a compiler
34 getCompilerName: function ( templateName
) {
35 var nameParts
= templateName
.split( '.' );
36 if ( nameParts
.length
< 2 ) {
37 throw new Error( 'Template name must have a suffix' );
39 return nameParts
[ nameParts
.length
- 1 ];
43 * Get a compiler via its name.
45 * @param {string} name Name of a compiler
46 * @return {Object} The compiler
48 getCompiler: function ( name
) {
49 var compiler
= compilers
[ name
];
51 throw new Error( 'Unknown compiler ' + name
);
57 * Register a template associated with a module.
59 * Precompiles the newly added template based on the suffix in its name.
61 * @param {string} moduleName Name of the ResourceLoader module the template is associated with
62 * @param {string} templateName Name of the template (including suffix)
63 * @param {string} templateBody Contents of the template (e.g. html markup)
64 * @return {Object} Compiled template
66 add: function ( moduleName
, templateName
, templateBody
) {
67 // Precompile and add to cache
68 var compiled
= this.compile( templateBody
, this.getCompilerName( templateName
) );
69 if ( !compiledTemplates
[ moduleName
] ) {
70 compiledTemplates
[ moduleName
] = {};
72 compiledTemplates
[ moduleName
][ templateName
] = compiled
;
78 * Get a compiled template by module and template name.
80 * @param {string} moduleName Name of the module to retrieve the template from
81 * @param {string} templateName Name of template to be retrieved
82 * @return {Object} Compiled template
84 get: function ( moduleName
, templateName
) {
88 if ( compiledTemplates
[ moduleName
] && compiledTemplates
[ moduleName
][ templateName
] ) {
89 return compiledTemplates
[ moduleName
][ templateName
];
92 moduleTemplates
= mw
.templates
.get( moduleName
);
93 if ( !moduleTemplates
|| !moduleTemplates
[ templateName
] ) {
94 throw new Error( 'Template ' + templateName
+ ' not found in module ' + moduleName
);
97 // Compiled and add to cache
98 return this.add( moduleName
, templateName
, moduleTemplates
[ templateName
] );
102 * Compile a string of template markup with an engine of choice.
104 * @param {string} templateBody Template body
105 * @param {string} compilerName The name of a registered compiler
106 * @return {Object} Compiled template
108 compile: function ( templateBody
, compilerName
) {
109 return this.getCompiler( compilerName
).compile( templateBody
);
113 // Register basic html compiler
114 mw
.template
.registerCompiler( 'html', {
115 compile: function ( src
) {
117 render: function () {
118 return $( $.parseHTML( $.trim( src
) ) );
124 }( mediaWiki
, jQuery
) );