Paozhu logoPaozhu Docs
教程目录
DEV

视图与模板

模板放在 view/,由 CLI 编译成 C++ 方法,再由控制器渲染。

模板语法

模板使用 <%c ... %> 嵌入 C++ 代码,数据来自控制器的 client.val。

view/cms/list.htmlhtml
<html>
<body>
  <h1><%c echo obj["title"].to_string(); %></h1>
  <%c if (obj["list"].is_array()) { %>
    <%c for (auto &row : obj["list"].as_array()) { %>
      <p><%c echo row["name"].to_string(); %></p>
    <%c } %>
  <%c } %>
</body>
</html>

编译模板

terminalbash
# Build templates into viewsrc/
./bin/paozhu_cli view          # incremental
./bin/paozhu_cli view force    # full rebuild

# Then rebuild the C++ target
cd build && make -j8

控制器渲染

controller/src/article_list.cppcpp
auto articles = orm::cms::Article();
articles.where("isopen", 1).fetch();

client.val["title"] = "Article list";
client.val["list"].set_array();
for (auto &row : articles) {
    obj_val item;
    item["name"] = row.title;
    client.val["list"].push(std::move(item));
}
peer->view("cms/list");

多主题与 SaaS

theme_view() 会按站点的 themes 配置给视图路径加前缀;get_theme() 和 get_themeurl() 读取站点级静态资源前缀。这是 SaaS 多主题的基础。