Show HN:Rocal UI —— 一个简单的基于Rust的模板引擎
Show HN: Rocal UI – A simple template engine with Rust

原始链接: https://github.com/rocal-dev/rocal/tree/main/rocal_ui

Rocal UI是一个基于Rust的模板引擎,专为Rocal框架设计,但可在任何Rust项目中使用。其语法允许通过变量嵌入、条件逻辑和循环来动态生成HTML。 变量嵌入使用`{{ variable }}`,也可以使用返回`&str`的Rust表达式。`if-else`语句允许条件渲染,类似于Rust语法。对于迭代集合,使用`for-in`循环,与Rust的`for`循环结构类似。字符串字面量可以直接嵌入,使用`{ "string" }`。 模板可以嵌套,允许你在另一个模板中使用`view!`宏来包含部分模板。要使用它,请使用`cargo install rocal --features="cli"`安装Rocal CLI,并使用`rocal new -n yourapp`创建一个新项目。然后,`yourapp/src/templates/root_template.rs`文件将包含一个示例用法。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Show HN: Rocal UI – 一个简单的 Rust 模板引擎 (github.com/rocal-dev) 16 分,来自 picolt,1 天前 | 隐藏 | 过去 | 收藏 | 2 条评论 badmonster 1 天前 [–] 有计划添加内置组件或宏来处理常见的 UI 模式,例如模态框或表单验证辅助工具吗? 回复 picolt 21 小时前 | 父级 [–] 这些想法听起来不错。因为它是一个简单的模板引擎,所以我避免添加您提到的许多功能。 也许,如果您需要这些功能,您会喜欢 Yew 和 Leptos,它们是其他使用 Rust 的 UI 框架。https://github.com/yewstack/yew https://github.com/leptos-rs/leptos 回复 加入我们 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:

原文

logo

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>
}

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>
}
  • for-in: This can be used as same as Rust syntax
for article in articles {
  <li><a href={{ article.url }}>{{ article.title }}</a></li>
}
  • { "string" }: This is sort of shorthand of variable embedding for only string. You can use it with regular string, exactly, &str in Rust context

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>
}

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) }}.

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

$ cargo install rocal --features="cli"
$ rocal new -n yourapp

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

联系我们 contact @ memedata.com