Files
moloapp/example/fw/Templates.hpp
T

40 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
// Jinja-like template environment: extends plus blocks, includes, if, for,
// filters, autoescape for .html/.htm/.xml with explicit safe opt-out.
// Gap: real molo::templates in 05-request-routing.md owns the full lexer,
// parser, sandbox, and streamed rendering.
#include <map>
#include <string>
#include <variant>
#include <vector>
namespace molo::example::fw {
using TemplateList = std::vector<std::map<std::string, std::string>>;
using TemplateValue = std::variant<std::string, TemplateList>;
using TemplateCtx = std::map<std::string, TemplateValue>;
class Environment {
public:
void add_template(std::string name, std::string source);
void add_global(std::string key, std::string value);
bool has_template(const std::string& name) const;
std::string render(const std::string& name, TemplateCtx ctx) const;
private:
std::string render_source(const std::string& name, const std::string& src,
TemplateCtx& ctx, int depth) const;
std::string render_blocks(const std::string& src, TemplateCtx& ctx,
int depth) const;
std::map<std::string, std::string> templates_;
std::map<std::string, std::string> globals_;
};
std::string html_escape(std::string_view in);
} // namespace molo::example::fw