Skip to main content

element

Attribute Macro element 

Source
#[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)]
#[zyn::element(debug = "pretty")]   // requires `pretty` feature
#[zyn::element("alias", debug)]
ZYN_DEBUG="Greeting" cargo build

Without 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.3", features = ["pretty"] }