Crates.io | yew-askama |
lib.rs | yew-askama |
version | 0.1.0 |
source | src |
created_at | 2023-11-19 15:32:37.23768 |
updated_at | 2023-11-19 15:32:37.23768 |
description | Create Yew components from Askama templates. |
homepage | https://github.com/peterhenryd/yew-askama |
repository | https://github.com/peterhenryd/yew-askama |
max_upload_size | |
id | 1041256 |
size | 7,972 |
This crate provides an attribute that allows you to easily turn Askama templates into Yew components.
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.