def test_dynamic_key_with_imports(self): lookup = TemplateLookup() lookup.put_string( "foo.html", """ <%! callcount = [0] %> <%namespace file="ns.html" import="*"/> <%page cached="True" cache_key="${foo}"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """, ) lookup.put_string("ns.html", """""") t = lookup.get_template("foo.html") m = self._install_mock_cache(t) t.render(foo="somekey") t.render(foo="somekey") assert result_lines(t.render(foo="somekey")) == [ "this is foo", "callcount: [1]", ] assert m.kwargs == {}
def _get_template(template_file: str) -> mako.template.Template: directories = [ str( pathlib.Path(appdirs.user_config_dir('online-judge-tools')) / 'template'), pkg_resources.resource_filename('onlinejudge_template_resources', 'template'), ] lookup = mako.lookup.TemplateLookup(directories=directories, input_encoding="utf-8", output_encoding="utf-8") path = pathlib.Path(template_file) has_slash = path.name != template_file # If template_file has path separators or any other things characteristic to paths, we use it as a path. This is a similar behavior to searching commands in shell. if has_slash and path.exists(): with open(path, "rb") as fh: lookup.put_string(template_file, fh.read()) template = lookup.get_template(template_file) logger.info('use template file: %s', template.filename) return template
def test_dynamic_key_with_imports(self): lookup = TemplateLookup() lookup.put_string("foo.html", """ <%! callcount = [0] %> <%namespace file="ns.html" import="*"/> <%page cached="True" cache_key="${foo}"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """) lookup.put_string("ns.html", """""") t = lookup.get_template("foo.html") m = self._install_mock_cache(t) t.render(foo='somekey') t.render(foo='somekey') assert result_lines(t.render(foo='somekey')) == [ "this is foo", "callcount: [1]" ] assert m.kwargs == {}