aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-12-04 09:52:35 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-12-04 09:52:35 +0100
commit0501ee7b850936db0e518dc734e62db13cd07006 (patch)
tree15ae4eac42c1b012628fbaacbf8a93f4ee5f359b /src
parent0ff26657d59715ac875fb7949a58a08039b5739e (diff)
downloadunja-0501ee7b850936db0e518dc734e62db13cd07006.tar.gz
unja-0501ee7b850936db0e518dc734e62db13cd07006.zip
add context_free function for easily getting rid of context
Diffstat (limited to 'src')
-rw-r--r--src/template.c34
1 files changed, 22 insertions, 12 deletions
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