-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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") |
|
mrozigor
reviewed
Dec 16, 2021
introduced by initializing `execute_result`
--- 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())));
} |
--- 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") |
mrozigor
approved these changes
Dec 17, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for the optional mustache feature "Lambdas", including value expansion.
It will most likely require extensive testing and review.
fixes #36