r712 - in funjs/public_html: . funjs
Author: jruchaud Date: 2014-06-18 16:40:19 +0200 (Wed, 18 Jun 2014) New Revision: 712 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/712 Log: Begin parse in template engine with variable Modified: funjs/public_html/funjs/TemplateEngine.js funjs/public_html/index.html Modified: funjs/public_html/funjs/TemplateEngine.js =================================================================== --- funjs/public_html/funjs/TemplateEngine.js 2014-06-18 13:24:07 UTC (rev 711) +++ funjs/public_html/funjs/TemplateEngine.js 2014-06-18 14:40:19 UTC (rev 712) @@ -1,17 +1,53 @@ TemplateEngine = Class.extend({ - - load : function(scriptId, node) { + + REGEXP_VAR : /{{(\??)(.+?)}}/g, + + load: function(scriptId, node, data) { + var self = this; + return $scriptReader.read(scriptId) - .done(function(content) { - - if (typeof node == "string") { - var element = $id(node); - } else { - element = node; - } - $log(element) - element.innerHTML = content; + .done(function(content) { + + if (typeof node == "string") { + var element = $id(node); + } else { + element = node; + } + + element.innerHTML = content; + self.parse(element, data || {}); }); + }, + + parse: function(root, data) { + this.parseElement(root, data); + + var children = root.childNodes, + l = children.length; + + for (var i = 0; i < l; i++) { + var child = children[i]; + + if (child instanceof HTMLElement) { + this.parse(child, data); + + } else { + this.parseText(child, data); + } + } + }, + + parseElement: function(element, data) { +// $log("Element", element) + }, + + parseText: function(element, data) { + var text = element.nodeValue; + element.nodeValue = text.replace(this.REGEXP_VAR, function(match, g1, g2) { + if (g1 == "?" && !data[g2]) return ""; // Optionnal value + + return data[g2]; + }); } - + }); \ No newline at end of file Modified: funjs/public_html/index.html =================================================================== --- funjs/public_html/index.html 2014-06-18 13:24:07 UTC (rev 711) +++ funjs/public_html/index.html 2014-06-18 14:40:19 UTC (rev 712) @@ -14,7 +14,7 @@ <script id="hello" type="text/html"> <div id="hello"> - <p>Hello World!</p> + <p>Hello World {{?from}} to {{?to}}!</p> </div> </script>
participants (1)
-
jruchaud@users.nuiton.org