From a0486e27504338dc6ce65fdecfce2cc4caabd8e1 Mon Sep 17 00:00:00 2001 From: Pixel Date: Tue, 29 Nov 2011 16:46:53 -0800 Subject: Being a bit more laxist with Mustache's values, and accepting almost anything as a variable; also adding a checkTemplate() method to assert a few things onto a template after loading it. --- includes/SimpleMustache.h | 3 +++ src/SimpleMustache.cc | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/includes/SimpleMustache.h b/includes/SimpleMustache.h index 5d0b9cf..66db3ca 100644 --- a/includes/SimpleMustache.h +++ b/includes/SimpleMustache.h @@ -80,6 +80,7 @@ class SimpleMustache { void setTemplate(const String & str) { setTemplate((const uint8_t *) str.to_charp(), str.strlen()); } void render(IO h, Context * ctx) { Assert(ctx); render_r(h, ctx, "", m_fragments.begin(), false, -1); } void empty() { while (!m_fragments.empty()) { delete m_fragments.front(); m_fragments.pop_front(); } } + void checkTemplate() { Fragments::iterator end = checkTemplate_r(m_fragments.begin()); Assert(end == m_fragments.end()); } ~SimpleMustache() { empty(); } private: struct Fragment { @@ -99,6 +100,8 @@ class SimpleMustache { Fragments::iterator render_r(IO h, Context * ctx, const String & endSection, Fragments::iterator begin, bool noWrite, int forceIdx); String escape(const String & s); + + Fragments::iterator checkTemplate_r(Fragments::iterator begin, const String & endSection = ""); }; }; diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index 6ed8401..1288286 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -328,6 +328,24 @@ void Balau::SimpleMustache::setTemplate(IO _h) { m_fragments.push_back(curFragment); } +Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::checkTemplate_r(Fragments::iterator begin, const String & endSection) { + Fragments::iterator cur; + Fragments::iterator end = m_fragments.end(); + + for (cur = begin; cur != end; cur++) { + Fragment * fr = *cur; + if ((fr->type == Fragment::END_SECTION) && (endSection.strlen() != 0)) { + Assert(fr->str == endSection); + return cur; + } + Assert(fr->type != Fragment::END_SECTION); + if ((fr->type == Fragment::SECTION) || (fr->type == Fragment::INVERTED)) + cur = checkTemplate_r(++cur, fr->str); + } + + return end; +} + Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::render_r(IO h, Context * ctx, const String & endSection, Fragments::iterator begin, bool noWrite, int forceIdx) { Fragments::iterator cur; Fragments::iterator end = m_fragments.end(); @@ -382,16 +400,20 @@ Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::render_r(IOfind(fr->str); if (f != sCtx->end()) { Context * var = f->second; - Assert(var->m_type == Context::STRING); - h->write(escape(var->m_str)); + if (var->m_type == Context::STRING) + h->write(escape(var->m_str)); + else if (var->m_type == Context::BOOLSEC) + h->write(var->m_bool ? "true" : "false"); } break; case Fragment::NOESCAPE: f = sCtx->find(fr->str); if (f != sCtx->end()) { Context * var = f->second; - Assert(var->m_type == Context::STRING); - h->write(var->m_str); + if (var->m_type == Context::STRING) + h->write(var->m_str); + else if (var->m_type == Context::BOOLSEC) + h->write(var->m_bool ? "true" : "false"); } break; case Fragment::SECTION: -- cgit v1.2.3