aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Vagrantfile7
-rw-r--r--src/template.c34
-rw-r--r--tests/data/template-with-logic/base.tmpl9
-rw-r--r--tests/data/template-with-logic/child.tmpl10
-rw-r--r--tests/test_template.c8
5 files changed, 53 insertions, 15 deletions
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