React 中的手柄栏模板

Bha*_*nan 2 handlebars.js reactjs

目前我们正在将一个应用程序从 jquery 迁移到 react。在现有的应用程序中,handlebar 用于生成少量 html,然后将其进一步发送到服务器进行 pdf 生成。是否可以在 REact 应用程序中重新使用车把。基本上我们想使用车把模板并将其与来自 React 应用程序后端的 Json 一起使用,是否有可能,如果有的话,是否有任何指针?

Yon*_*zhi 5

它实际上与 React 没有任何关系。如果我正确理解您的问题,您希望在客户端(在浏览器中)运行车把。答案是肯定的,您绝对可以在 React 应用程序中导入 Handlebar 并编译模板。

import Handlebars from "handlebars";

const hbr = `
<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>
`;

const template = Handlebars.compile(hbr);
const htmlString = template(data);
Run Code Online (Sandbox Code Playgroud)

https://codesandbox.io/s/sweet-http-c4565为例。