Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
HtmlTemplateView Class Reference

#include <HtmlTemplateView.h>

+ Inheritance diagram for HtmlTemplateView:
+ Collaboration diagram for HtmlTemplateView:

Public Member Functions

 HtmlTemplateView (const std::string &source, TemplateSource mode=TemplateSource::Inline)
 
std::string render (const std::map< std::string, std::string > &context={}) const override
 Render the view body.
 
void render (HttpRequest &req, HttpResponse &res)
 
std::string getContentType () const override
 Return the MIME content type for this view.
 
- Public Member Functions inherited from FrameworkView
virtual ~FrameworkView ()=default
 
virtual void applyHeaders (HttpResponse &response) const
 Optional hook to set response headers (e.g., Content-Disposition).
 
virtual std::string render (const nlohmann::json &context) const
 Optional JSON context hook (used by JsonView or convenience).
 

Private Attributes

std::string templateSource_
 
std::string filePath_
 
TemplateSource mode_
 

Detailed Description

Definition at line 25 of file HtmlTemplateView.h.

Constructor & Destructor Documentation

◆ HtmlTemplateView()

HtmlTemplateView::HtmlTemplateView ( const std::string &  source,
TemplateSource  mode = TemplateSource::Inline 
)

Definition at line 10 of file HtmlTemplateView.cpp.

11 : mode_(mode)
12{
14 {
15 templateSource_ = source;
16 }
17 else
18 {
19 filePath_ = source;
20 }
21}
TemplateSource mode_
std::string filePath_
std::string templateSource_

References filePath_, Inline, mode_, and templateSource_.

Member Function Documentation

◆ getContentType()

std::string HtmlTemplateView::getContentType ( ) const
overridevirtual

Implements FrameworkView.

Definition at line 59 of file HtmlTemplateView.cpp.

60{
61 return "text/html";
62}

◆ render() [1/2]

std::string HtmlTemplateView::render ( const std::map< std::string, std::string > &  context = {}) const
overridevirtual

Optional context (used by templates).

Implements FrameworkView.

Definition at line 23 of file HtmlTemplateView.cpp.

24{
25 std::string tpl;
26
28 {
29 tpl = templateSource_;
30 }
31 else
32 {
33 auto *storage = AppContext::get<StorageManager>();
34 std::vector<uint8_t> buf;
35 if (storage && storage->readFile(filePath_, buf))
36 {
37 tpl.assign(buf.begin(), buf.end());
38 }
39 else
40 {
41 tpl = "<h1>Template not found</h1>";
42 }
43 }
44
45 for (const auto &[key, value] : context)
46 {
47 std::string placeholder = "{{" + key + "}}";
48 size_t pos = 0;
49 while ((pos = tpl.find(placeholder, pos)) != std::string::npos)
50 {
51 tpl.replace(pos, placeholder.length(), value);
52 pos += value.length();
53 }
54 }
55
56 return tpl;
57}
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91

References filePath_, AppContext::getTypeKey(), Inline, mode_, and templateSource_.

Referenced by render().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ render() [2/2]

void HtmlTemplateView::render ( HttpRequest req,
HttpResponse res 
)
inline

Definition at line 31 of file HtmlTemplateView.h.

31 {
32 res.setContentType("text/html");
33 res.send(render());
34 }
std::string render(const std::map< std::string, std::string > &context={}) const override
Render the view body.
HttpResponse & setContentType(const std::string &content_type)
Set the Content-Type header.
void send(const std::string &body)
Send a full response including headers and body.

References render(), HttpResponse::send(), and HttpResponse::setContentType().

+ Here is the call graph for this function:

Member Data Documentation

◆ filePath_

std::string HtmlTemplateView::filePath_
private

Definition at line 40 of file HtmlTemplateView.h.

Referenced by HtmlTemplateView(), and render().

◆ mode_

TemplateSource HtmlTemplateView::mode_
private

Definition at line 41 of file HtmlTemplateView.h.

Referenced by HtmlTemplateView(), and render().

◆ templateSource_

std::string HtmlTemplateView::templateSource_
private

Definition at line 39 of file HtmlTemplateView.h.

Referenced by HtmlTemplateView(), and render().


The documentation for this class was generated from the following files: