Skip to main content

Crate hemplate

Crate hemplate 

Source
Expand description

hemplate: facade crate for the hemplate template language.

hemplate-runtime is always available. The default host feature exposes AST/build APIs, while the independent derive feature exposes #[derive(Hemplate)]. Constrained targets can disable defaults and enable only derive, keeping parser and Tree-sitter dependencies host-only.

Dynamic native HTML Boolean attributes require a bool: false omits the attribute, true emits its name. Strings such as "false" are not accepted. Global HTML Booleans also apply to custom and unknown HTML elements.

HTML +hidden is different: it binds an enumerated attribute using HiddenAttribute. Boolean false omits it, true emits its name, and string states (including "until-found" and "false") remain escaped values. References forward these semantics. Generic templates must require HiddenAttribute, not just Display. Foreign content keeps ordinary escaped value interpolation; ambiguous namespaces reject Boolean and +hidden bindings. Use ordinary hidden="{+ value +}" for purely textual values there.

Escaping protects text, not attributes the browser reads as code. A plain value interpolated into on*, style, srcdoc, a script or stylesheet URL, or as the whole value of a URL attribute does not compile; use Url::parse, a type implementing the context’s trait (UrlValue, ScriptValue, …), or an explicit Trusted value. Fixed text that settles a URL’s destination (href="/users/{+ id +}") takes plain values. See the repository README, “Untrusted values and attribute contexts”.

ⓘ
use hemplate::Hemplate;
// templates/boolean_attrs.heml includes <input +checked="self.enabled">.
#[derive(Hemplate)]
struct BooleanAttrs {
    enabled: &'static str,
    value: &'static str,
    hidden: bool,
}
impl BooleanAttrs {
    fn next_hidden(&self) -> &bool { &self.hidden }
}

A generic Display bound cannot silently stringify a Boolean hidden value:

ⓘ
use hemplate::{HiddenAttribute, HiddenValue};
fn hidden_state<T: core::fmt::Display>(value: &T) -> HiddenValue<'_> {
    HiddenAttribute::hidden_value(value)
}

Modules§

ast
AST types and parsing functions.
context
Attribute contexts and the typed values that may enter them. Attribute contexts and the typed values that may enter them.
error
Error types. Error type for rendering already-generated templates.
escape
HTML escape utility. HTML escaping utility.
hidden
Typed values for HTML’s enumerated hidden attribute. Typed values for HTML’s enumerated hidden attribute.
render
Codegen dispatch helpers for interpolation. Codegen dispatch helpers for {+ expr +} and {+= expr =+}.

Structs§

ExtractionError
An error that occurs during AST node extraction.
HtmlEscape
Wrapper that HTML-escapes the Display output of the inner value.
ParseError
An error that occurs during template parsing.
Srcset
A srcset whose candidates are all checked Urls.
Trusted
A value someone vouches for in context C, without any check.
Url
A URL that is relative or has an allowed scheme: http, https, mailto or tel.

Enums§

AnimationContext
SVG animation value context marker.
BuildError
Top-level error type for host-side template processing.
Descriptor
How large a srcset candidate renders.
DocumentContext
srcdoc context marker.
HiddenValue
The output state of an HTML +hidden binding.
HtmlContext
Raw HTML ({+= =+}) context marker.
NavigationContext
<base href> and <meta http-equiv> context marker.
RenderError
A failure while rendering an already-generated template.
ResourceContext
Script, style and plug-in loading URL context marker.
ScriptContext
Script (on*) context marker.
SrcsetContext
srcset context marker.
StyleContext
style context marker.
UrlContext
URL context marker.
UrlError
Why a value is not accepted as a Url or Srcset part.

Traits§

AnimationValue
A value that may be placed in to, from, by or values of an SVG animation.
BooleanAttribute
A value for a dynamic HTML Boolean attribute such as +disabled.
Context
Marker for a context that Trusted values can be created for.
DocumentValue
A document that may be placed in srcdoc.
Hemplate
Trait for types that can render themselves as an HTML string.
HiddenAttribute
A value supported by HTML +hidden bindings.
HtmlValue
Markup that raw interpolation {+= value =+} may insert unescaped.
NavigationValue
A value that may be placed in <base href> or <meta http-equiv>.
ResourceValue
A URL that may load script, styles or plug-ins.
ScriptValue
JavaScript that may be placed in an on* attribute.
SrcsetValue
A value that may be the whole value of a srcset attribute.
StyleValue
CSS that may be placed in a style attribute.
UrlValue
A value that may be the whole value of a URL attribute.

Type Aliases§

TrustedAnimation
An SVG animation value vouched for without a check.
TrustedDocument
A document vouched for without a check.
TrustedHtml
HTML markup vouched for without a check, for raw interpolation {+= =+}.
TrustedNavigation
A navigation value vouched for without a check.
TrustedResource
A script, style or plug-in URL vouched for without a check.
TrustedScript
JavaScript vouched for without a check.
TrustedSrcset
A srcset vouched for without a check.
TrustedStyle
CSS vouched for without a check.
TrustedUrl
A URL vouched for without a check, for example a data: or sms: URL.

Derive Macros§

Hemplate