|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file TemplateView.hpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. |
| 7 | + * All rights reserved. |
| 8 | + * https://github.com/vixcpp/vix |
| 9 | + * |
| 10 | + * Use of this source code is governed by a MIT license |
| 11 | + * that can be found in the License file. |
| 12 | + * |
| 13 | + * Vix.cpp |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef VIX_VIEW_TEMPLATE_VIEW_HPP |
| 18 | +#define VIX_VIEW_TEMPLATE_VIEW_HPP |
| 19 | + |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include <vix/http/Response.hpp> |
| 24 | +#include <vix/template/Context.hpp> |
| 25 | +#include <vix/template/Engine.hpp> |
| 26 | + |
| 27 | +namespace vix::view |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @brief HTTP-integrated template rendering facade. |
| 31 | + * |
| 32 | + * TemplateView is the bridge between: |
| 33 | + * - the Vix runtime HTTP layer |
| 34 | + * - the template engine |
| 35 | + * |
| 36 | + * Responsibilities: |
| 37 | + * - render templates by name |
| 38 | + * - convert render results into native HTTP responses |
| 39 | + * - expose streaming rendering for large outputs |
| 40 | + */ |
| 41 | + class TemplateView |
| 42 | + { |
| 43 | + public: |
| 44 | + /** |
| 45 | + * @brief Construct a TemplateView from a template engine. |
| 46 | + * |
| 47 | + * @param engine Shared template engine instance. |
| 48 | + */ |
| 49 | + explicit TemplateView( |
| 50 | + std::shared_ptr<vix::template_::Engine> engine); |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Render a template to a string. |
| 54 | + * |
| 55 | + * @param name Template name. |
| 56 | + * @param context Runtime rendering context. |
| 57 | + * @return Rendered HTML string. |
| 58 | + */ |
| 59 | + [[nodiscard]] std::string render( |
| 60 | + const std::string &name, |
| 61 | + const vix::template_::Context &context) const; |
| 62 | + |
| 63 | + /** |
| 64 | + * @brief Render a template and produce a native HTTP response. |
| 65 | + * |
| 66 | + * @param name Template name. |
| 67 | + * @param context Runtime rendering context. |
| 68 | + * @return HTTP response containing rendered HTML. |
| 69 | + */ |
| 70 | + [[nodiscard]] vix::vhttp::Response render_response( |
| 71 | + const std::string &name, |
| 72 | + const vix::template_::Context &context) const; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Render a template in streaming mode. |
| 76 | + * |
| 77 | + * @tparam Output Output sink type. |
| 78 | + * @param name Template name. |
| 79 | + * @param context Runtime rendering context. |
| 80 | + * @param out Output sink receiving rendered chunks. |
| 81 | + */ |
| 82 | + template <typename Output> |
| 83 | + void render_stream( |
| 84 | + const std::string &name, |
| 85 | + const vix::template_::Context &context, |
| 86 | + Output &out) const |
| 87 | + { |
| 88 | + engine_->render_stream(name, context, out); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @brief Access the underlying template engine. |
| 93 | + * |
| 94 | + * @return Shared engine instance. |
| 95 | + */ |
| 96 | + [[nodiscard]] const std::shared_ptr<vix::template_::Engine> & |
| 97 | + engine() const noexcept; |
| 98 | + |
| 99 | + private: |
| 100 | + /** |
| 101 | + * @brief Shared template engine. |
| 102 | + */ |
| 103 | + std::shared_ptr<vix::template_::Engine> engine_; |
| 104 | + }; |
| 105 | + |
| 106 | +} // namespace vix::view |
| 107 | + |
| 108 | +#endif // VIX_VIEW_TEMPLATE_VIEW_HPP |
0 commit comments