From 615e648260a87eb2edabf4aefb456186800e67a1 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sat, 2 Aug 2014 10:46:00 +0900 Subject: mustache partial implementation --- template_test/Makefile | 2 +- template_test/mustachetest.cc | 13 ++++++++++--- template_test/test.py | 10 +++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) (limited to 'template_test') diff --git a/template_test/Makefile b/template_test/Makefile index f03e8fb..ad0d49b 100644 --- a/template_test/Makefile +++ b/template_test/Makefile @@ -1,5 +1,5 @@ all: - $(CXX) -std=c++11 -g -o mustachetest mustachetest.cc + $(CXX) -Wall -std=c++11 -g -o mustachetest mustachetest.cc .PHONY: clean clean: rm -f mustachetest *.o diff --git a/template_test/mustachetest.cc b/template_test/mustachetest.cc index 3b80b5d..c4ac2c9 100644 --- a/template_test/mustachetest.cc +++ b/template_test/mustachetest.cc @@ -11,15 +11,22 @@ using namespace crow::mustache; string read_all(const string& filename) { ifstream is(filename); - string ret; - copy(istreambuf_iterator(is), istreambuf_iterator(), back_inserter(ret)); - return ret; + return {istreambuf_iterator(is), istreambuf_iterator()}; } int main() { auto data = json::load(read_all("data")); auto templ = compile(read_all("template")); + auto partials = json::load(read_all("partials")); + set_loader([&](std::string name)->std::string + { + if (partials.count(name)) + { + return partials[name].s(); + } + return ""; + }); context ctx(data); cout << templ.render(ctx); return 0; diff --git a/template_test/test.py b/template_test/test.py index f99ca23..22fcca0 100755 --- a/template_test/test.py +++ b/template_test/test.py @@ -8,14 +8,17 @@ for testfile in glob.glob("*.json"): for test in testdoc["tests"]: if "lambda" in test["data"]: continue - if "partials" in test: - #print testfile, test["name"] - continue open('data', 'w').write(json.dumps(test["data"])) open('template', 'w').write(test["template"]) + if "partials" in test: + open('partials', 'w').write(json.dumps(test["partials"])) + else: + open('partials', 'w').write("{}") ret = subprocess.check_output("./mustachetest") print testfile, test["name"] if ret != test["expected"]: + if 'partials' in test: + print 'partials:', json.dumps(test["partials"]) print json.dumps(test["data"]) print test["template"] print 'Expected:',repr(test["expected"]) @@ -23,3 +26,4 @@ for testfile in glob.glob("*.json"): assert ret == test["expected"] os.unlink('data') os.unlink('template') + os.unlink('partials') -- cgit v1.2.3-54-g00ecf