Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mustache lambda support #299

Merged
merged 9 commits into from
Dec 17, 2021
Merged

Mustache lambda support #299

merged 9 commits into from
Dec 17, 2021

Conversation

The-EDev
Copy link
Member

This PR adds support for the optional mustache feature "Lambdas", including value expansion.

It will most likely require extensive testing and review.

fixes #36

@The-EDev The-EDev marked this pull request as draft December 10, 2021 03:46
@The-EDev The-EDev marked this pull request as ready for review December 10, 2021 23:38
@crow-clang-format
Copy link

--- examples/example.cpp	(before formatting)
+++ examples/example.cpp	(after formatting)
@@ -124,13 +124,15 @@
     });
 
     //same as the example above but uses mustache instead of stringstream
-    CROW_ROUTE(app,"/add_mustache/<int>/<int>")
-    ([](int a, int b){
-      auto t = crow::mustache::compile("Value is {{func}}");
-      crow::mustache::context ctx;
-      ctx["func"] = [&](std::string){return std::to_string(a+b);};
-      auto result = t.render(ctx);
-      return result;
+    CROW_ROUTE(app, "/add_mustache/<int>/<int>")
+    ([](int a, int b) {
+        auto t = crow::mustache::compile("Value is {{func}}");
+        crow::mustache::context ctx;
+        ctx["func"] = [&](std::string) {
+            return std::to_string(a + b);
+        };
+        auto result = t.render(ctx);
+        return result;
     });
 
     // Compile error with message "Handler type is mismatched with URL paramters"
--- tests/unittest.cpp	(before formatting)
+++ tests/unittest.cpp	(after formatting)
@@ -1141,12 +1141,14 @@
 
 TEST_CASE("template_function")
 {
-  auto t = crow::mustache::compile("attack of {{func}}");
-  crow::mustache::context ctx;
-  ctx["name"] = "killer tomatoes";
-  ctx["func"] = [&](std::string){return std::string("{{name}}, IN SPACE!");};
-  auto result = t.render(ctx);
-  CHECK("attack of killer tomatoes, IN SPACE!" == result);
+    auto t = crow::mustache::compile("attack of {{func}}");
+    crow::mustache::context ctx;
+    ctx["name"] = "killer tomatoes";
+    ctx["func"] = [&](std::string) {
+        return std::string("{{name}}, IN SPACE!");
+    };
+    auto result = t.render(ctx);
+    CHECK("attack of killer tomatoes, IN SPACE!" == result);
 }
 
 TEST_CASE("template_load")

@The-EDev
Copy link
Member Author

unittest.cpp shouldn't be formatted (at least not in this PR)

@CrowCpp CrowCpp deleted a comment from crow-clang-format bot Dec 14, 2021
include/crow/mustache.h Outdated Show resolved Hide resolved
@crow-clang-format
Copy link

crow-clang-format bot commented Dec 17, 2021

--- include/crow/mustache.h	(before formatting)
+++ include/crow/mustache.h	(after formatting)
@@ -222,20 +222,20 @@
                                         out += ctx.s;
                                     break;
                                 case json::type::Function:
+                                {
+                                    std::string execute_result = ctx.execute();
+                                    while (execute_result.find("{{") != std::string::npos)
                                     {
-                                        std::string execute_result = ctx.execute();
-                                        while (execute_result.find("{{") != std::string::npos)
-                                        {
-                                            template_t result_plug(execute_result);
-                                            execute_result = result_plug.render(*(stack[0]));
-                                        }
-
-                                        if (action.t == ActionType::Tag)
-                                            escape(execute_result, out);
-                                        else
-                                            out += execute_result;
+                                        template_t result_plug(execute_result);
+                                        execute_result = result_plug.render(*(stack[0]));
                                     }
-                                    break;
+
+                                    if (action.t == ActionType::Tag)
+                                        escape(execute_result, out);
+                                    else
+                                        out += execute_result;
+                                }
+                                break;
                                 default:
                                     throw std::runtime_error("not implemented tag type" + boost::lexical_cast<std::string>(static_cast<int>(ctx.t())));
                             }

@crow-clang-format
Copy link

--- tests/unittest.cpp	(before formatting)
+++ tests/unittest.cpp	(after formatting)
@@ -1141,12 +1141,14 @@
 
 TEST_CASE("template_function")
 {
-  auto t = crow::mustache::compile("attack of {{func}}");
-  crow::mustache::context ctx;
-  ctx["name"] = "killer tomatoes";
-  ctx["func"] = [&](std::string){return std::string("{{name}}, IN SPACE!");};
-  auto result = t.render(ctx);
-  CHECK("attack of killer tomatoes, IN SPACE!" == result);
+    auto t = crow::mustache::compile("attack of {{func}}");
+    crow::mustache::context ctx;
+    ctx["name"] = "killer tomatoes";
+    ctx["func"] = [&](std::string) {
+        return std::string("{{name}}, IN SPACE!");
+    };
+    auto result = t.render(ctx);
+    CHECK("attack of killer tomatoes, IN SPACE!" == result);
 }
 
 TEST_CASE("template_load")

@CrowCpp CrowCpp deleted a comment from crow-clang-format bot Dec 17, 2021
@The-EDev The-EDev merged commit 680fb4c into master Dec 17, 2021
@The-EDev The-EDev deleted the mustache_lambdas branch December 17, 2021 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement lambdas in mustache.h
2 participants