Converts {% include %} tag in django into javascript expression Examples: >>> from client_templates import TemplateJsNode, JsTplSettings >>> from django.template import Template >>> ############### >>> #This object is pretty hard to test using doc test as we need an entire environment to setup >>> #hence for the time being we won't test it :( >>> ############### """ expected_node_classname = 'ConstantIncludeNode' def _init_vars(self): tpl_path = self.django_node.template.name if tpl_path not in JsTplSettings.CONFIG['tpls']: raise NameError("Unabled to find template file %s in JsTplSettings.CONFIG[\'tpls\']" % tpl_path) tpl_info = JsTplSettings.CONFIG['tpls'][tpl_path] tpl_var_list = None self.tpl_func_name = tpl_info['tpl_func_name'] if 'var_list' in tpl_info: tpl_var_list = tpl_info['var_list'] self.tpl_var_list = tpl_var_list #add parameters of the include file into the global context of the calling file if tpl_var_list: [self.context.register_var(var_name, 'global') for var_name in tpl_var_list] self.update_parent_context() def generate_js_statement(self): return 'return %s' % self.generate_js_statement_as_closure() def generate_js_statement_as_closure(self): return self._wrap_expr_in_js_func(self.tpl_func_name, self.tpl_var_list) JsProcessorRegistry.register_js_node(ConstantIncludeJsNode)
empty_block_init = 'var ' + sequence_size_init + ';' else: for_init_vars.append(sequence_size_init) if JsTplSettings.CONFIG['VERSAGER_MODE']: for_init_vars.append(self.loop_val_varname + '=[]') loop_val = self.loop_val_varname + '[' + self.loop_val_varname + '.length]=' \ + self._nodes_to_js_str(self.loop_block, as_closure = True) loop_val_result = self.loop_val_varname + '.join("")' else: for_init_vars.append(self.loop_val_varname + '=""') loop_val = self.loop_val_varname + '+=' + self._nodes_to_js_str(self.loop_block, as_closure = True) loop_val_result = self.loop_val_varname for_init = 'var ' + ','.join(for_init_vars) + ';' #e.g. for(...; i < arraySize; ...) for_check = self.for_iterator_varname + '<' + self.sequence_size_varname + ';' #e.g. for(..; ...; i++) for_increment = self.for_iterator_varname + '++' for_stmt = 'for(' + for_init + for_check + for_increment + '){' + ','.join(js_loop_vars) + ';' + \ loop_val + '} return ' + loop_val_result #if empty block is set, then we wrap the for statement inside of if statement that tests whether or not the #sequence is empty if self.empty_block: for_stmt = empty_block_init + 'if(' + self.sequence_size_varname + '){' + for_stmt + \ '}else{' + self._nodes_to_js_str(self.empty_block) + '}' return for_stmt JsProcessorRegistry.register_js_node(ForJsNode)
#YZ JavaScript Django Template Compiler #Copyright (c) 2010 Weiss I Nicht <*****@*****.**> #(sha-1: 90f01291285340bf03c2d41952c4b21b3d338907) from client_templates import BaseJsFilter, JsProcessorRegistry class DefaultJsFilter(BaseJsFilter): """ Converts the "default" filter in django templates to javascript expression i.e. {{exampleVar|default:"default value"}} Examples: >>> from client_templates import TemplateJsNode,JsTplSettings >>> JsTplSettings.CONFIG['VERSAGER_MODE'] = False >>> ############### >>> #test django "default" filter >>> ############### >>> js_tpl = TemplateJsNode('Default value text: {{testVar|default:"default val"}}') >>> js_tpl.render() u'function(testVar){return "Default value text: "+yzdjs_default(testVar,"default val")}' """ expected_filter_funcname = 'default' js_func_name = 'yzdjs_default' file_path = __file__ JsProcessorRegistry.register_js_filter(DefaultJsFilter)
#YZ JavaScript Django Template Compiler #Copyright (c) 2010 Weiss I Nicht <*****@*****.**> #(sha-1: 90f01291285340bf03c2d41952c4b21b3d338907) from client_templates import BaseJsFilter, JsProcessorRegistry class HashJsFilter(BaseJsFilter): """ Converts the "hash" filter in django templates to native javascript hash look up, i.e. {{exampleVar|hash:varB}} Examples: >>> from client_templates import TemplateJsNode,JsTplSettings >>> from client_templates.customfilters import * >>> JsTplSettings.CONFIG['VERSAGER_MODE'] = False >>> ############### >>> #test django "hash" filter >>> ############### >>> js_tpl = TemplateJsNode('{% load hash %}Dict var1 with hash varB{{ varA|hash:varB }}') >>> js_tpl.render() u'function(varA,varB){return "Dict var1 with hash varB"+varA[varB]}' """ expected_filter_funcname = 'hash' def render(self): return '%s[%s]' % (self.expr, self.arg) JsProcessorRegistry.register_js_filter(HashJsFilter)
#YZ JavaScript Django Template Compiler #Copyright (c) 2010 Weiss I Nicht <*****@*****.**> #(sha-1: 90f01291285340bf03c2d41952c4b21b3d338907) from client_templates import BaseJsFilter, JsProcessorRegistry class AddJsFilter(BaseJsFilter): """ Converts the "add" filter in django templates to native javascript expression, i.e. {{exampleVar|add:"2"}} Examples: >>> from client_templates import TemplateJsNode,JsTplSettings >>> JsTplSettings.CONFIG['VERSAGER_MODE'] = False >>> ############### >>> #test django "add" filter >>> ############### >>> js_tpl = TemplateJsNode('1 + 1 = {{ 1|add:"1" }}') >>> js_tpl.render() u'function(){return "1 + 1 = "+(1+1)}' >>> js_tpl = TemplateJsNode('{{ 1|add:"1" }}') >>> js_tpl.render() u'function(){return (1+1)}' """ expected_filter_funcname = 'add' def render(self): return '(%s)' % '+'.join(self.js_func_params) JsProcessorRegistry.register_js_filter(AddJsFilter)
>>> from client_templates.customtags import * >>> JsTplSettings.CONFIG['VERSAGER_MODE'] = False >>> ############### >>> #test django AssignNode object behaves as expected >>> ############### >>> from django.template import Template >>> django_tpl = Template('{%load assign%} example here: {% assign other_str "another"|capfirst %}') >>> assign_node = django_tpl.nodelist[2] >>> assign_node.name u'other_str' >>> assign_node.value.var u'another' >>> assign_node.value.filters[0][0].__name__ 'capfirst' >>> ############### >>> #test assign js node works >>> ############### >>> js_tpl = TemplateJsNode('{%load assign%} assigning here: {% assign other_str varA|add:"5" %} displaying here: {{ other_str }}') >>> js_tpl.render() u'function(varA){var other_str;return " assigning here: "+function(){other_str=(varA+5);return ""}()+" displaying here: "+other_str}' """ expected_node_classname = 'AssignNode' def _init_vars(self): self.new_var_name = self.context.register_var(self.django_node.name, scope="global", var_type="implicit") self.new_var_value_expr = self._extract_filter_expression(self.django_node.value) def generate_js_statement(self): #e.g. varA=123 #e.g. function(){varA=123;return null}() return self.new_var_name + '=' + self.new_var_value_expr + ';return ""' JsProcessorRegistry.register_js_node(AssignJsNode)