DEV Community

Cover image for Rocal UI - A simple template engine with Rust🦀
Yoshi
Yoshi

Posted on

1 1 1 1 1

Rocal UI - A simple template engine with Rust🦀

Hi developers, as I introduced Rocal - Full-Stack WASM framework in my previous article, today I'm really pleased to show you guys "Rocal UI - A simple template engine with Rust".

Although this template engine is basically intended to use with Rocal framework to craft views, it can be used anywhere with Rust.

Let's begin with syntax of Rocal UI. Here is a simple example including variables, if-else control, and for-loop control.

view! {
  <div class="container">
    <h1 class="title">{{ title }}</h1>

    if user.id <= 10 {
      <p>{ "You are an early user!" }</p>
      <a href={{ reward_url }}>{ "Click here to get rewards!" }</a>
    } else if user.id <= 20 {
      <p>{ "You are kind of an early user." }</p>
      <a href={{ sort_of_reward_url }}>{ "Check it out for your reward." }</a>
    } else {
      <p>{ "You are a regular user." }</p>
    }

    <hr/>

    <ul>
      for article in articles {
        <li><a href={{ article.url }}>{{ article.title }}</a></li>
      }
    </ul>
  </div>
}
Enter fullscreen mode Exit fullscreen mode

It's straight forward, isn't it?

  • {{ variable }} : as you saw the code above, you can use any variable with it
  • if-else : you can utilize if-else even else-if as below
if user.id <= 10 {
    <p>{ "You are an early user!" }</p>
    <a href={{ reward_url }}>{ "Click here to get rewards!" }</a>
} else if user.id <= 20 {
   <p>{ "You are kind of an early user." }</p>
   <a href={{ sort_of_reward_url }}>{ "Check it out for your reward." }</a>
} else {
  <p>{ "You are a regular user." }</p>
}
Enter fullscreen mode Exit fullscreen mode
  • for-in: This can be used as same as Rust syntax
for article in articles {
  <li><a href={{ article.url }}>{{ article.title }}</a></li>
}
Enter fullscreen mode Exit fullscreen mode
  • { "string" }: This is sort of shorthand of variable embedding for only string. You can use it with regular string, exactly, &str in Rust context

Advanced use

view! {} produces HTML string technically, so you can embed view! in another view! like using it as a partial template.

let button = view! {
  <button type="submit" class="btn btn-primary">
    Submit
  </button>
};

view! {
  <form action={{ &format!("/articles/{}", article.id) }}>
    <input type="text" />
    {{ &button }}
  </form>
}
Enter fullscreen mode Exit fullscreen mode

On top of that, so {{ variable }} can take any expression that emits &str of Rust, if you want to do string interpolation, you can write like {{ &format!("Hi, {}", name) }}.

And more...

fn button(ty: &str, class: &str, label: &str) -> String {
  view! {
    <button type={{ ty }} class={{ class }}>
      {{ label }}
    </button>
  }
}

view! {
  <form action="/posts">
    <input type="text" />
    {{ &button("submit", "btn btn-primary", "Submit") }}
  </form>
}
Enter fullscreen mode Exit fullscreen mode

Can be used like a component as well.

How to install

On MacOS or Linux

$ curl -fsSL https://www.rocal.dev/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

On Windows
(if you have not had cargo yet, follow this link first)

$ cargo install rocal --features="cli"
$ rocal new -n yourapp
Enter fullscreen mode Exit fullscreen mode

Then in yourapp/src/templates/root_template.rs, you could see an example of usage of Rocal UI

Links

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay