Using HTML Templates at server side - html

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?

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.

Related

Convert JSON Plus JSON Schema into raw HTML?

I've found a lot of solutions that use on-the-fly scripting to convert JSON and JSON-Schema's to a human-readable form.
What I need, however, is a way to produce the raw HTML without any dynamic elements on the server side.
This form will need to be a read-only HTML view of the JSON object.
Options#1:
Take a JSON-Schema and convert it to a pure HTML template.
Store the pre-generated HTML Template.
Then, for each JSON I pass it and the template to a method (maybe w/ some css).
And finally get raw HTML.
Option#2:
Store the JSON-Schema.
Pass the JSON-Schema and the raw JSON to a method (maybe w/ some css).
And finally get raw HTML.

How to post massive data to html with Flask

I am building a python web app with Flask.
I got 3 lists (same length) with about 3,000 data each from my python code.
And like to make them into a table in the html file,
how should I write the html template (the tr, td things).
I could only find tutorial about passing one single variable to html.
If there are any references for me to study I will appreciate that a lot.
You can use javascript to achieve this.
Create a rest api with some url like http://yoursite/data which gives this large data in json format.
Then use a ajax calls to fetch data and use javascript/angular/jquery script to fill the data into html table.
Some refs:
https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
Using jQuery to build table rows from Ajax response (Json)
Populate html table on jquery success event
Since Flask uses Jinja2, you can take Jinja2 Documentation as reference.

HTML form data submitted to embedded mongodb

I want to insert data into a mongodb collection from a HTML Form and I want it to have a specific structure. e.g.
{name:{first_name:"", last_name:""}, address:{street:"",
post_code:""}}
In a normal form submission the data doesn't seem to have structure. Any ideas?
Thanks!
HTML cannot insert into MongoDB directly, you need an application layer, if your application is based on java, you can map the form values into an object of your choice and then save that object in MongoDB.
If you are using nodeJS, it is even easier where you can form the said structure on the frontend and make an ajax call to node server and it can save the passed request as it is into MongoDB.
If you must use HTML form, I think you will have to do the hard work of parsing the HTML data to object of desired structure.

Parse APEX RESTful WebService reference JSON response stored in CLOB001 column

I am trying to generate a report page in my APEX application, using the data obtained from a REST service response.
I added a new RESTful webservice reference and sepecified a JSON output.
Then I've generated a query/report page, but this is what's being currently displayed:
Instead I want the report to display the contents of data field on the JSON response (A single row with various columns and values)
Is there any straight forward way to show each response element and field on its row and column instead of a single column and row with the whole response, like there is with XML RESTful responses?
Consider whether another output type may be more handy. If you're putting out JSON and expect to handle it in PLSQL, then you do need to realize that PLSQL does not handle that natively.
Want to handle the rest response as json in plsql? Then look into some PLSQL libraries/projects that may do this:
http://reseau.erasme.org/pl-sql-library-for-JSON?lang=en
http://sourceforge.net/projects/pljson/
Then print out the returned HTML through HTP.P calls.
Though honestly, if you want to stick with plsql you would be much better off using XML as a return type, since you can then use all the xml goodies in the database. Though simply spitting out some html structure may not be quite too interesting (but I digress).
Or call the rest service through an ajax call from javascript, and handle the object there. After all, JSON is Javascript Object Notation and should be perfect for javascript, right? Then simply inject your html somewhere in the document. Ideally you would set up a region as a container for this.

Best way to sanitize JSON from Rails

RoR 3 automagically sanitizes ERB templates (when done correctly). However, I've got a little project where I'm using RoR for the application tier only and javascript for the presentation. So, typical request is ajax call to rails route and render returned json. Issue is it is currently possible for me to inject js, create a new product with title <script>alert('hello')</script> and this is returned as is on the next request and the browser happily interprets the script.
Is it best to
sanitize the inputs on post?
sanitize the json response on the server? (override to_json?)
sanitize the json response on the client?
I appreciate any input.
You should encode HTML entities on the content client-side as you're appending data to the page.
The question is, do your other product fields intentionally contain markup like links or paragraph tags that will also be encoded? If this is the case, and you intend to render some parts of the json response as HTML on the page, then you should be sanitizing your input at the point when new products are created, and limited the HTML tags you allow to a specific subset, and then scrubbing away their attributes. There are libraries to automate this process, like the sanitize gem.