From 6fe2cd6ec1062fdf250bed41bc034e87c7d1278d Mon Sep 17 00:00:00 2001 From: Stefano Fiorentino Date: Fri, 22 Feb 2019 12:10:18 +0100 Subject: [PATCH 1/3] adding convenient api to access std::map::count Signed-off-by: Stefano Fiorentino --- include/plustache/context.hpp | 1 + src/context.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/plustache/context.hpp b/include/plustache/context.hpp index 183c700..7ea18f9 100644 --- a/include/plustache/context.hpp +++ b/include/plustache/context.hpp @@ -19,6 +19,7 @@ namespace Plustache { int add(const std::string& key, const PlustacheTypes::ObjectType& o); int add(const PlustacheTypes::ObjectType& o); PlustacheTypes::CollectionType get(const std::string& key) const; + int count(const std::string &key) const; private: /* data */ diff --git a/src/context.cpp b/src/context.cpp index fac54ef..285e30d 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -131,3 +131,13 @@ PlustacheTypes::CollectionType Context::get(const std::string& key) const } return ret; } + +/** + * + * @param key + * @return + */ +int Context::count(const std::string &key) const +{ + return ctx.count(key); +} From 4ef0f5dde3d6135bf01231d5962975225ff2d612 Mon Sep 17 00:00:00 2001 From: Stefano Fiorentino Date: Fri, 22 Feb 2019 12:11:02 +0100 Subject: [PATCH 2/3] using the flag __not_to_escape to avoid escaping strings Signed-off-by: Stefano Fiorentino --- src/template.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/template.cpp b/src/template.cpp index b740372..d50e38e 100644 --- a/src/template.cpp +++ b/src/template.cpp @@ -124,7 +124,10 @@ std::string template_t::render_tags(const std::string& tmplate, { try { - repl.assign(template_t::html_escape(ctx.get(key)[0][key])); + if (!ctx.count("__not_to_escape")) + { repl.assign(template_t::html_escape(ctx.get(key)[0][key])); } + else + { repl.assign(ctx.get(key)[0][key]); } } catch(int i) { repl.assign(""); } } From b3ca1776b1510924540b6e9e8d217ae88cf37f86 Mon Sep 17 00:00:00 2001 From: Stefano Fiorentino Date: Fri, 22 Feb 2019 13:56:50 +0100 Subject: [PATCH 3/3] remove trailing commas on __remove_tailing_comma context flag Signed-off-by: Stefano Fiorentino --- src/template.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/template.cpp b/src/template.cpp index d50e38e..242c4ca 100644 --- a/src/template.cpp +++ b/src/template.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -239,6 +240,14 @@ std::string template_t::render_sections(const std::string& tmplate, small_ctx.add(*it); repl += template_t::render_tags(matches[3], small_ctx); } + if (ctx.count("__remove_trailing_comma")) + { + boost::algorithm::trim(repl); + while (boost::algorithm::ends_with(repl, ",")) + { + repl.pop_back(); + } + } } } else repl.assign("");