#[element]Expand description
Defines a reusable template component generating a struct that implements Render.
Function parameters become either props (struct fields passed at call site)
or extractors (marked #[zyn(input)], resolved automatically from Input).
Built-in extractor types: Extract<T>, Attr<T>, Fields, Variants, Data<T>.
A parameter named children receives the inner token stream from a children block.
§Examples
Simple element with props:
#[zyn::element]
fn greeting(name: syn::Ident) -> zyn::TokenStream {
zyn::zyn!(pub fn {{ name }}() {})
}
// Invoke inside a template
zyn::zyn!(@greeting(name = format_ident!("hello")))
// output: pub fn hello() {}Element with an extractor and a children block:
#[zyn::element]
fn wrapper(
#[zyn(input)] ident: zyn::Extract<syn::Ident>,
children: zyn::TokenStream,
) -> zyn::TokenStream {
zyn::zyn!(impl {{ ident }} { {{ children }} })
}
zyn::zyn!(@wrapper { fn new() -> Self { Self } })
// output: impl MyStruct { fn new() -> Self { Self } }Optional custom name alias (defaults to function name):
#[zyn::element("my_alias")]
fn internal_name(label: syn::Ident) -> zyn::TokenStream { ... }
zyn::zyn!(@my_alias(label = format_ident!("x")))§Debugging
Add debug to inspect the generated code as a compiler note diagnostic.
Requires the ZYN_DEBUG environment variable to match the generated struct
name (supports * wildcards, e.g., ZYN_DEBUG="*").
#[zyn::element(debug)] // body only, raw
#[zyn::element(debug(pretty))] // body only, pretty-printed (requires `pretty` feature)
#[zyn::element(debug(full))] // full struct + impl, raw
#[zyn::element(debug(pretty, full))] // full struct + impl, pretty-printed
#[zyn::element(debug(name = "Foo"))] // inject prop, raw
#[zyn::element(debug(pretty, name = "Foo"))] // inject prop, pretty-printed
#[zyn::element("alias", debug)] // with custom name aliasZYN_DEBUG="Greeting" cargo buildWithout ZYN_DEBUG, the debug argument is inert — safe to leave in source.
The pretty format uses prettyplease for formatted output. Enable it with:
zyn = { version = "0.4", features = ["pretty"] }