DOM Operation Takes Background json Problem

  node.js, question

http://runjs.cn/detail/n5vshadv

The code is the above link = = a lot of it is redundant. I will briefly describe the problem below.

Html is swig template rendering, similar to django’s

My function is to fetch data from the background when rendering this template, and the background data is transmitted in json form.

What I have to do is to get the data transmitted backstage and then output it to the page. json format and id or name are corresponding. The simple correspondence is as follows:
Json: name foreground: name
Json:a.name foreground: a_name

So, as I wrote, if you want to fill #contct_email with data, the code is$('#contact_email').val(data.result.contact.email);
Similarly, if A is filled with data, the code should be$('#a').val(data.result.a);

Then the problem arises. I can’t write one by one, although it can also be = = is there any way to simplify it without writing so many duplicate codes?

The code above get_json was given to me by others and can be ignored or referred to. The main idea is to take data from option to the background and fill it in the corresponding option. The background is also Json format. The transformation of. and _ is implemented in the background. The code is as follows:

coffeescript.put '/user/:id', (req, res) ->
 # Tips: id number and password cannot be modified
 id = req.params.id
 data = req.body
 doc = {}
 return res.sendStatus 400 if not _.validate rules.objectID, id
 return res.sendStatus 403 if req.session.userInfo?  ._id isnt id and req.session.role isnt 'super'
 {total,invalidate} = userInfoAndRule.reduce ((x,y) ->
 [field,rule] = y
 if field.replace(/\./g, '_') of data
 X.total plus = 1
 if _.validate rule, data[field]
 doc[field] = data[field]
 else
 X.invalidate plus = 1
 x
 ), total:0,invalidate:0
 res.sendStatus 400 if invalidate or not total
 db.updateUserInfo id, doc, (err, succeeded) ->
 if err
 console.warn err
 return res.sendStatus 500
 if not succeeded
 return res.sendStatus 404
 #TODO: Update the information in the session
 res.sendStatus 200

I just want to steal a lazy, ask the great god to explain

Since it is a template, it is definitely not filled one by one.
Usually the template will have a render function, you can refer toOfficial documents;
Page rendering includes background rendering and front-end rendering. You get data through ajax and then render the page, which belongs to front-end rendering.

I haven’t used swig before. I wrote a small example in the reference document for reference only:
图片描述