diff --git a/include/minja/minja.hpp b/include/minja/minja.hpp index 5ed0556..1ee175f 100644 --- a/include/minja/minja.hpp +++ b/include/minja/minja.hpp @@ -2725,8 +2725,15 @@ inline std::shared_ptr Context::builtins() { globals.set("raise_exception", simple_function("raise_exception", { "message" }, [](const std::shared_ptr &, Value & args) -> Value { throw std::runtime_error(args.at("message").get()); })); - globals.set("tojson", simple_function("tojson", { "value", "indent" }, [](const std::shared_ptr &, Value & args) { - return Value(args.at("value").dump(args.get("indent", -1), /* to_json= */ true)); + // Accept optional keyword arguments to match common Jinja usage such as + // tojson(indent=2) and tojson(ensure_ascii=False). The ensure_ascii flag + // is ignored since nlohmann::json dumps UTF-8 by default, but it is + // accepted for compatibility to avoid "Unknown argument" errors. + globals.set("tojson", simple_function("tojson", { "value", "indent", "ensure_ascii" }, [](const std::shared_ptr &, Value & args) { + // Read indent if provided; ignore ensure_ascii if present. + const auto indent = args.get("indent", -1); + // bool ensure_ascii = args.get("ensure_ascii", true); // unused + return Value(args.at("value").dump(indent, /* to_json= */ true)); })); globals.set("items", simple_function("items", { "object" }, [](const std::shared_ptr &, Value & args) { auto items = Value::array(); diff --git a/tests/test-syntax.cpp b/tests/test-syntax.cpp index 36bdaa3..a3e2398 100644 --- a/tests/test-syntax.cpp +++ b/tests/test-syntax.cpp @@ -80,6 +80,8 @@ TEST(SyntaxTest, SimpleCases) { EXPECT_EQ("bcXYZab", render("{{ 'abcXYZabc'.strip('ac') }}", {}, {})); EXPECT_EQ(R"(["a", "b"])", render("{{ 'a b'.split(' ') | tojson }}", {}, {})); + // Accept ensure_ascii kwarg for compatibility, ignored by implementation + EXPECT_EQ(R"("test")", render(R"({{ 'test' | tojson(ensure_ascii=False) }})", {}, {})); EXPECT_EQ( "Ok",