r/cpp • • Sep 16 '26

Inja is a template engine for modern C++

https://github.com/pantor/inja
61 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 Bioinformatican 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.

1

u/FckXFckMusk Sep 18 '26

Apologies but would you have any links to these examples you specified, if not, no worries but thanks for your excellent answer..

3

u/guepier Bioinformatican Sep 18 '26

Not sure what you mean by examples. But virtually all large, traditional websites are running on top of such an engine1. All widely-used server-side web frameworks (Spring MVC, ASP.NET, Laravel, Symfony, Django, Flask, Ruby on Rails, …) use a template engine to render the output that is sent to the client.

Another example is Sweave/RMarkdown/Quarto. These are all closely-related template languages that are used to render reports from data scientific analyses (usually using the R programming language). This is usually a two-step process: in the first step, the template engine executes the inline code and produces a Markdown file; in the second step, this Markdown file is converted to HTML, PDF, DOCX, or similar. Here’s a concrete example. You can see the source code by going on the ā€œCodeā€ button at the top and selecting ā€œView Sourceā€Ā in the dropdown.


1 SPAs work somewhat differently

-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.