Jinja2Cpp
Jinja2Cpp copied to clipboard
Add usage samples
Add usage samples for:
- [x] Web (based on boost.beast)
- [ ] Code generation (based on cppast)
@Manu343726 , would you like to add your cppast/tinyrefl template examples here?
Whenever I make it work, sure ;)
Thanks!
BTW something like this should work right now:
#include <cppast/libclang_parser.hpp>
#include <tinyrefl/tool/model/cppast/jinja2reflection.hpp>
#include <jinja2cpp/template.h>
using namespace tinyrefl::tool::model;
int main()
{
using parser_t = cppast::simple_file_parser<cppast::libclang_parser>;
cppast::cpp_entity_index index;
parser_t parser{type_safe::ref(index)};
parser_t::config config;
config.set_flags(cppast::cpp_standard::cpp_11);
auto file = parser.parse("my_enum.hpp", config);
jinja2::ValuesMap variables{
{"file", jinja2::Reflect(make_entity_ref(index, file.value()))}};
jinja2::Template tmpl;
tmpl.Load(R"(
{% for enum in file.enums %}
{{enum.fullname}} from_string(const std::string& str)
{
{% for value in enum.values %}
if((str == "{{value.name}}") ||
(str == "{{value.qualifiedname}}") ||
(str == "{{value.fullname}}"))
{
return {{value.fullname}};
}
{% endfor %}
throw std::logic_error{"Invalid {{enum.fullname}} value name"};
}
{% endfor %}
)");
std::ofstream output{"my_enum_from_string.cpp"};
tmpl.Render(output, variables);
}
Contemporary classics. :D