comparison UnitTestsSources/Plustache.cpp @ 825:d8f5de5b9517 templating

partials
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 May 2014 17:54:59 +0200
parents da4c30a8bcdd
children
comparison
equal deleted inserted replaced
823:da4c30a8bcdd 825:d8f5de5b9517
1 #include "gtest/gtest.h" 1 #include "gtest/gtest.h"
2 2
3 #include <include/template.hpp> 3 #include <include/template.hpp>
4 4
5 TEST(Plustache, Basic) 5
6 class OrthancPlustache : public Plustache::template_t
6 { 7 {
7 std::map<std::string, std::string> ctx; 8 public:
9 protected:
10 virtual std::string get_template(const std::string& tmpl)
11 {
12 //printf("OK [%s]\n", tmpl.c_str());
13 return Plustache::template_t::get_template(tmpl);
14 }
15
16 virtual std::string get_partial(const std::string& partial) const
17 {
18 //printf("OK2 [%s]\n", partial.c_str());
19 //return Plustache::template_t::get_partial(partial);
20 return "<li>{{name}}</li>";
21 }
22 };
23
24
25 TEST(Plustache, Basic1)
26 {
27 PlustacheTypes::ObjectType ctx;
8 ctx["title"] = "About"; 28 ctx["title"] = "About";
9 29
10 Plustache::template_t t; 30 OrthancPlustache t;
11 ASSERT_EQ("<h1>About</h1>", t.render("<h1>{{title}}</h1>", ctx)); 31 ASSERT_EQ("<h1>About</h1>", t.render("<h1>{{title}}</h1>", ctx));
12 } 32 }
13 33
34
35 TEST(Plustache, Basic2)
36 {
37 Plustache::Context ctx;
38 ctx.add("title", "About");
39
40 OrthancPlustache t;
41 ASSERT_EQ("<h1>About</h1>", t.render("<h1>{{title}}</h1>", ctx));
42 }
43
44
45 TEST(Plustache, Context)
46 {
47 PlustacheTypes::ObjectType a;
48 a["name"] = "Orthanc";
49
50 PlustacheTypes::ObjectType b;
51 b["name"] = "Jodogne";
52
53 PlustacheTypes::CollectionType c;
54 c.push_back(a);
55 c.push_back(b);
56
57 Plustache::Context ctx;
58 ctx.add("items", c);
59
60 OrthancPlustache t;
61 ASSERT_EQ("<ul><li>Orthanc</li><li>Jodogne</li></ul>",
62 t.render("<ul>{{#items}}<li>{{name}}</li>{{/items}}</ul>", ctx));
63 }
64
65
66 TEST(Plustache, Partials)
67 {
68 PlustacheTypes::ObjectType a;
69 a["name"] = "Orthanc";
70
71 PlustacheTypes::ObjectType b;
72 b["name"] = "Jodogne";
73
74 PlustacheTypes::CollectionType c;
75 c.push_back(a);
76 c.push_back(b);
77
78 Plustache::Context ctx;
79 ctx.add("items", c);
80
81 OrthancPlustache t;
82 ASSERT_EQ("<ul><li>Orthanc</li><li>Jodogne</li></ul>",
83 t.render("<ul>{{#items}}{{>partial}}{{/items}}</ul>", ctx));
84 }
85
86