简体   繁体   English

在服务器端使用HTML模板

[英]Using HTML Templates at server side

I am working on play framework with SCALA as backend. 我正在使用SCALA作为后端开发播放框架。 Json data is given to the front end from the controller. Json数据从控制器提供给前端。

I want to add HTML as value of some fields of json. 我想添加HTML作为json的某些字段的值。 This HTML will be kept as a template and data will be added in this template at run time. 该HTML将作为模板保存,并且数据将在运行时添加到该模板中。

I think i should put unique names in the HTML template and then these names will be replaced by the data which i want to add at run time. 我认为我应该在HTML模板中放置唯一的名称,然后这些名称将替换为我想在运行时添加的数据。 Ultimately, this HTML will be added in the json response. 最终,此HTML将添加到json响应中。

Is my approach right? 我的方法正确吗? If not, what is the best approach to add data in an HTML template,add this template in json response and send this combined response to the front-end for further use? 如果不是,最好的方法是在HTML模板中添加数据,在json响应中添加此模板并将此组合响应发送到前端以供进一步使用?

Is it a good practice to use string replacement to add data in an HTML template? 使用字符串替换在HTML模板中添加数据是一种好习惯吗?

I think as long as you use Play, you can put your HTML templates into app/views package. 我认为,只要您使用Play,就可以将HTML模板放入应用程序/视图包中。 Let's say you call your template mytemplate.scala.html You can parameterize this view as any Play view. 假设您将模板称为mytemplate.scala.html,您可以将此视图参数化为任何Play视图。 In the place in your code where you generate your JSON response you can then call mytemplate(parameters) to get html generated, Play will do all the work here for you. 在代码中生成JSON响应的地方,您可以调用mytemplate(parameters)生成html,Play将在这里为您完成所有工作。 Then using play.api.libs.json.JSON object's methods and related facilities you can convert this html to JSON. 然后使用play.api.libs.json.JSON对象的方法和相关功能,可以将此html转换为JSON。 So in your controller's code you will have something like Ok(JSON.toJson(mytemplate(parameters))) 因此,在控制器的代码中,您将获得类似于Ok(JSON.toJson(mytemplate(parameters)))的内容。

This is of course a sketch, so you will need to elaborate and try. 这当然是草图,因此您需要详细说明和尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM