user189107
user189107

Reputation: 187

Using HTML Templates at server side

I am working on play framework with SCALA as backend. Json data is given to the front end from the controller.

I want to add HTML as value of some fields of json. This HTML will be kept as a template and data will be added in this template at run time.

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. Ultimately, this HTML will be added in the json response.

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?

Is it a good practice to use string replacement to add data in an HTML template?

Upvotes: 0

Views: 304

Answers (1)

Alexander Arendar
Alexander Arendar

Reputation: 3425

I think as long as you use Play, you can put your HTML templates into app/views package. Let's say you call your template mytemplate.scala.html You can parameterize this view as any Play view. 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. Then using play.api.libs.json.JSON object's methods and related facilities you can convert this html to JSON. So in your controller's code you will have something like Ok(JSON.toJson(mytemplate(parameters)))

This is of course a sketch, so you will need to elaborate and try.

Upvotes: 1

Related Questions