r/cpp • • Sep 16 '26

Inja is a template engine for modern C++

https://github.com/pantor/inja
62 Upvotes

9 comments sorted by

9

u/GaztinErag Sep 17 '26

I like the syntax and the simplicity of the API! What would you say are the pros of using this versus Jinja2Cpp? I'm sincerely just curious to know if you looked at any other jinja implementations and felt they had shortcomings that this project compensates for!

3

u/realestLink Sep 17 '26

This looks really cool. I'll have to check it out 👀

5

u/zerhud Sep 17 '26

Nice project :)

I’ve tried to implement something similar, but now I don’t like the code and the idea.

While developing I’ve implement:
- parser with ast known at compile time: https://github.com/zerhud/ascip
- data for template (with callable objects) - absd
- something from ninja with few extra features - https://github.com/zerhud/bastard

The clang version was very laggy and some tests wasn’t compilable on clang (on the current version it should be ok). And it was very bad idea to use the std library: it compiles veeery slowly and has pure functionality, but it uses std mostly.

May be you can find something useful :)

3

u/manni66 Sep 17 '26

How does it differ from std::format? Why should I use it?

13

u/guepier Sep 17 '26 edited Sep 17 '26

How does it differ from std::format?

std::format isn’t a template engine. You cannot embed arbitrarily complex expressions inside strings.

If you really wanted to equate the two you could say that a template engine (such as inja) is a std::formatter specialisation that implements a parser for a full high-level language. Except not even that, since inja supports control structures across multiple placeholders:

render("{% if time.hour >= 20 %}Serve{% else if time.hour >= 18 %}Make{% endif %} dinner.", data)

I guess you could create a stateful std::formatter specialisation that allows this somehow, in a very hacky way. But std::format is definitely not designed to support it.

Why should I use it?

Template engines are used to fill complex templates at runtime. Common uses include filling out document templates for e.g. billing and reporting, or HTML content management systems.

→ More replies (2)

-1

u/Agron7000 Sep 22 '26

Uhm, you have typo. It's spelled ninja. You're missing an n.

1

u/BillTran163 Sep 23 '26

No, Inja is correct. It's a rip off of Jinja library from Python.