DenisKolodin / yew
- вторник, 26 декабря 2017 г. в 03:15:03
Rust framework for making client web apps
Yew is a modern Rust framework inspired by Elm and ReactJS.
This framework designed to be compiled into modern browsers' runtimes: WASM, Asm.js, emscripten.
struct Model { }
enum Msg {
DoIt,
}
fn update(model: &mut Model, msg: Msg) {
match msg {
Msg::DoIt => {
// Update your model on events
}
}
}
fn view(model: &Model) -> html::Html<Msg> {
html! {
// Render your model here
<button onclick=|_| Msg::DoIt,></div>
}
}
Yew framework uses own virtual-dom representation.
html!
macroPut pure Rust code into html tags.
html! {
<section class="todoapp",>
<header class="header",>
<h1>{ "todos" }</h1>
{ view_input(&model) }
</header>
<section class="main",>
<input class="toggle-all",
type="checkbox",
checked=model.is_all_completed(),
onclick=|_| Msg::ToggleAll, />
{ view_entries(&model) }
</section>
</section>
}
Use single-line or multi-line Rust comments inside html-templates.
html! {
<section>
/* Write some ideas
* in multiline comments
*/
<p>{ "and tags could be placed between comments!" }</p>
// <li>{ "or single-line comments" }</li>
</section>
}
extern crate chrono;
use chrono::prelude::*;
fn view(model: &Model) -> Html<Msg> {
html! {
<p>{ Local::now() }</p>
}
}
There are two examples to check how it works: counter and todomvc.
To start them you should enter to a directory of any and start it with cargo-web:
$ cargo web start
Also you need a target to your Rust compiler for generating web outputs. By default
cargo-web uses asmjs-unknown-emscripten
. If you haven't one install it with:
$ rustup target add asmjs-unknown-emscripten