supla-device
Loading...
Searching...
No Matches
Supla::HtmlTag Class Reference

RAII helper for emitting a single HTML tag. More...

#include <web_sender.h>

Public Member Functions

 HtmlTag (WebSender *sender, const char *tagName, bool paired=true)
 HtmlTag (const HtmlTag &)=delete
HtmlTag & operator= (const HtmlTag &)=delete
 HtmlTag (HtmlTag &&other) noexcept
HtmlTag & operator= (HtmlTag &&other) noexcept
HtmlTag & attr (const char *name, const char *value)
 Append a quoted HTML attribute with escaped value.
HtmlTag & attr (const char *name, int value)
 Append a quoted HTML attribute with integer value.
HtmlTag & attr (const char *name, int value, int precision)
 Append a quoted HTML attribute with formatted numeric value.
HtmlTag & attrIf (const char *name, bool enabled)
 Append a boolean HTML attribute when enabled.
HtmlTag & close ()
 Emit the closing > for the opening tag.
HtmlTag & finish ()
 Close the opening tag and mark it as finished.
template<typename Fn>
void body (Fn &&fn)
 Emit a paired body using a callback.
void body (char *text)
 Emit a text body using escaped content.
template<size_t N>
void body (const char(&text)[N])
 Emit a text body using escaped content.
void body (const char *text)
 Emit a text body using escaped content.
void end ()
 Explicitly finish the tag.

Detailed Description

RAII helper for emitting a single HTML tag.

The object starts a tag immediately in the constructor: HtmlTag(sender, "div") emits <div.

Attributes can then be appended with chained calls. The tag is closed by:

  • calling close() to emit >
  • calling body() to emit > and a paired closing tag
  • letting the object go out of scope, which calls end() as a fallback

Typical usage:

sender->tag("div")
.attr("class", "form-field")
.body([&]() {
sender->labelFor("ssid", "SSID");
sender->textInput("ssid", "ssid");
});

For void elements such as <input>, use WebSender::voidTag().

Member Function Documentation

◆ attr()

HtmlTag & Supla::HtmlTag::attr ( const char * name,
int value,
int precision )

Append a quoted HTML attribute with formatted numeric value.

This is useful for attributes such as value, min, max or step when the number must be emitted with fixed precision.

◆ attrIf()

HtmlTag & Supla::HtmlTag::attrIf ( const char * name,
bool enabled )

Append a boolean HTML attribute when enabled.

Example: checked, selected, readonly.

◆ body()

template<typename Fn>
void Supla::HtmlTag::body ( Fn && fn)
inline

Emit a paired body using a callback.

The helper closes the opening tag, executes the callback and then emits the corresponding closing tag.

◆ end()

void Supla::HtmlTag::end ( )

Explicitly finish the tag.

For paired tags this emits the closing tag if needed. Calling this is optional for normal scope-based usage.

◆ finish()

HtmlTag & Supla::HtmlTag::finish ( )

Close the opening tag and mark it as finished.

For paired tags this prevents the destructor from emitting a closing tag. It is mainly useful for void tags and for low-level control when the caller wants to manage the content separately.


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