yew-askama

Crates.ioyew-askama
lib.rsyew-askama
version0.1.0
sourcesrc
created_at2023-11-19 15:32:37.23768
updated_at2023-11-19 15:32:37.23768
descriptionCreate Yew components from Askama templates.
homepagehttps://github.com/peterhenryd/yew-askama
repositoryhttps://github.com/peterhenryd/yew-askama
max_upload_size
id1041256
size7,972
Peter Deverdzic (peterhenryd)

documentation

README

yew-askama

This crate provides an attribute that allows you to easily turn Askama templates into Yew components.

Usage

This crate exports one public member: the template_component procedural macro attribute. This macro can be applied to structures, and is minimally invasive, since it does not redefine the struct, but rather edits it.

Take this example:

#[template_component]
#[template(path = "card.html")
pub struct Card {
    title: &'static str,
    content: &'static str,
}

which is (excluding some imports) transformed into:

#[derive(askama::Template, yew::Properties, PartialEq)]
#[template(path = "card.html")
pub struct CardTemplate {
    title: &'static str,
    content: &'static str,
}

#[function_component]
pub fn Card(template: &CardTemplate) -> Html {
    let template_string = template.render().unwrap();
    let attr_value = AttrValue::from_str(template_string.as_str()).unwrap();
    
    Html::from_html_unchecked(attr_value)
}

The template_component attribute requires Askama's template attribute to be defined. See here for more information.

Note that Askama's Template trait and Yew's Properties trait are derived onto the same type. This means you must consider the limitations of both traits, such as not being able to use lifetimes.

Limitations

  • Stateless: Since the components are defined using a structure, there is currently no way to run any code from the component in the code. This is not an inherent limitation, as this feature could be implemented (pretty easily) using implementations.
  • Non-selective visibility: While not an urgent issue, the visibility for the Yew component is currently not configurable.
Commit count: 2

cargo fmt