From 0501ee7b850936db0e518dc734e62db13cd07006 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Fri, 4 Dec 2020 09:52:35 +0100 Subject: add context_free function for easily getting rid of context --- Vagrantfile | 7 ++++--- src/template.c | 34 ++++++++++++++++++++----------- tests/data/template-with-logic/base.tmpl | 9 ++++++++ tests/data/template-with-logic/child.tmpl | 10 +++++++++ tests/test_template.c | 8 ++++++++ 5 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 tests/data/template-with-logic/base.tmpl create mode 100644 tests/data/template-with-logic/child.tmpl diff --git a/Vagrantfile b/Vagrantfile index 104f577..dd3f6c1 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,8 +1,9 @@ # A basic Vagrant box to be able to use GDB and Valgrind for debugging # Usage: -# 1. Start the machine using `vagrant up` -# 2. SSH into the machine using `vagrant ssh` -# 3. +# 1. Start the machine: `vagrant up` +# 2. SSH into the machine: `vagrant ssh` +# 3. Move into the project directory: `cd /vagrant` +# 4. Run find_memleaks on the binary you want to check: `find_memleaks bin/test_template` # # # -*- mode: ruby -*- diff --git a/src/template.c b/src/template.c index 105a8c3..a92edd6 100644 --- a/src/template.c +++ b/src/template.c @@ -690,32 +690,39 @@ struct hashmap *default_filters() { return filters; } +struct context context_new(struct hashmap *vars, struct env *env, struct template *current_tmpl) { + struct context ctx; + ctx.filters = default_filters(); + ctx.vars = vars; + ctx.env = env; + ctx.current_template = current_tmpl; + return ctx; +} + +void context_free(struct context ctx) { + hashmap_free(ctx.filters); +} + char *template_string(char *tmpl, struct hashmap *vars) { #if DEBUG printf("Template: %s\n", tmpl); #endif - mpc_ast_t *ast = parse(tmpl); - struct context ctx; - ctx.filters = default_filters(); - ctx.vars = vars; - ctx.env = NULL; - ctx.current_template = NULL; + struct mpc_ast_t *ast = parse(tmpl); + struct context ctx = context_new(vars, NULL, NULL); char *output = render_ast(ast, &ctx); mpc_ast_delete(ast); + context_free(ctx); return output; } char *template(struct env *env, char *template_name, struct hashmap *vars) { struct template *t = hashmap_get(env->templates, template_name); - #if DEBUG printf("Template name: %s\n", t->name); printf("Parent: %s\n", t->parent ? t->parent : "None"); #endif - struct context ctx; - ctx.vars = vars; - ctx.env = env; - ctx.current_template = t; + + struct context ctx = context_new(vars, env, t); // find root template while (t->parent != NULL) { @@ -728,5 +735,8 @@ char *template(struct env *env, char *template_name, struct hashmap *vars) { } } - return render_ast(t->ast, &ctx); + + char *output = render_ast(t->ast, &ctx); + context_free(ctx); + return output; } \ No newline at end of file diff --git a/tests/data/template-with-logic/base.tmpl b/tests/data/template-with-logic/base.tmpl new file mode 100644 index 0000000..241c78c --- /dev/null +++ b/tests/data/template-with-logic/base.tmpl @@ -0,0 +1,9 @@ +Header + +{%- block content %} +Content +{%- endblock %} + +{%- block footer %} +Footer +{%- endblock -%} \ No newline at end of file diff --git a/tests/data/template-with-logic/child.tmpl b/tests/data/template-with-logic/child.tmpl new file mode 100644 index 0000000..42687b6 --- /dev/null +++ b/tests/data/template-with-logic/child.tmpl @@ -0,0 +1,10 @@ +{% extends "base.tmpl" %} + +{% block content -%} + {{ "Hello World" | lower }} + {% if 2 < 1 -%} + 2 is less than 1. + {%- else -%} + 2 is more than 1. + {%- endif %} +{%- endblock %} \ No newline at end of file diff --git a/tests/test_template.c b/tests/test_template.c index 1ad131e..e85eec2 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -491,4 +491,12 @@ TEST(filter_title) { free(output); } +TEST(inheritance_depth_2) { + struct env *env = env_new("./tests/data/template-with-logic/"); + char *output = template(env, "child.tmpl", NULL); + assert_str(output, "Header\n\thello world\n\t2 is more than 1.\nFooter\n"); + free(output); + env_free(env); +} + END_TESTS \ No newline at end of file -- cgit v1.2.3