pub struct ViewHandle { /* private fields */ }Expand description
A handle to an HTML fragment.
A view handle may contain multiple sibling nodes, but opened tags must be closed so the fragment can be nested safely inside a larger document.
<!-- Valid: all tags are closed, safe to nest -->
<div>Hello</div>
<p>World</p>
<!-- Invalid: unclosed tag would corrupt the parent document -->
<div>HelloThe outermost View returns a self-contained handle.
It can be stored, sent to another task, or inserted into another view.
Nested views return handles tied to the enclosing build. Those handles
can only be rendered or inserted while that build is active.
Implementations§
Source§impl ViewHandle
impl ViewHandle
Sourcepub fn size_hint(&self) -> usize
pub fn size_hint(&self) -> usize
Returns an estimate of the number of bytes the view writes when rendered.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the view is statically known to render no output.
A view holding an instruction block reports false even when the
block happens to write nothing.
Sourcepub const fn unescaped_unchecked(body: &'static str) -> Self
pub const fn unescaped_unchecked(body: &'static str) -> Self
Creates a view from a &'static str without escaping it and without checking for syntax
errors.
Sourcepub fn render(self, cx: &Cx) -> String
pub fn render(self, cx: &Cx) -> String
Renders the view into an HTML string.
Status codes and headers declared in the view are discarded;
render_response collects them.
§Panics
Panics if the view is a nested handle rendered outside the build it belongs to, or if a dynamic attribute key or element name in the view contains a character that could break out of the identifier.
Sourcepub fn render_response(self, cx: &Cx) -> RenderedResponse
Available on crate feature http only.
pub fn render_response(self, cx: &Cx) -> RenderedResponse
http only.Renders the view into HTML together with the status code and response headers declared in it.
A view declares response metadata by placing an
http::StatusCode, an http::HeaderMap,
or a single (HeaderName, HeaderValue) pair in the node position of
the view! macro. Competing declarations resolve by render order:
the first status code rendered wins, and the first part that mentions
a header name provides all of that name’s values.
§Panics
Panics if the view is a nested handle rendered outside the build it belongs to, or if a dynamic attribute key or element name in the view contains a character that could break out of the identifier.
Sourcepub fn render_into(self, cx: &Cx, f: &mut Formatter<'_>)
pub fn render_into(self, cx: &Cx, f: &mut Formatter<'_>)
Writes the view’s output through f.
The formatter appends to its destination as it goes, so reserve the
size_hint up front to avoid reallocations. A
nested handle renders against the buffer of the build it belongs to,
which must be the one running on the current task.
§Panics
Panics if the view is a nested handle and no build is running on the current task, or a different one is, or if a dynamic attribute key or element name in the view contains a character that could break out of the identifier.