3

I am having sample template like below i want to replace school, babydet.name,dept,babydet.section.secname with json object values how to do this please help me to solve this.

var Template1 = 'Welcome to the {{school}} baby {{babydet.name}}'
var Template2 = 'Welcome to the school {{school}} department {{dept}} babydetails {{babydet.name}} {{babydet.section.secname}}'

const namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
const namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

with help of string replace need to achieve this(single logic). Output want like this "Welcome to the world baby shanker"

user7620655
  • 41
  • 2
  • 4
  • 1
    JSON is a *textual notation* for data exchange. [(More.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. Those are just objects. – T.J. Crowder Mar 06 '17 at 03:25

4 Answers4

4

If by any chance that lodash.js is available, you can use the _.template function for it. More details here.

var Template1 = 'Welcome to the {{school}} baby {{babydet.name}}'
var Template2 = 'Welcome to the school {{school}} department {{dept}} babydetails {{babydet.name}} {{babydet.section.secname}}'

var namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
var namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var interpolator = _.template(Template1);
var interpolated = interpolator(namelist2[0]);

console.log(interpolated)

interpolator = _.template(Template2);
interpolated = interpolator(namelist2[0]);

console.log(interpolated)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Adrian
  • 1,597
  • 12
  • 16
  • Please keep in mind that `_.template` also allows to evaluate JavaScript code which could introduce severe security risk for unescaped input. [See possible issue](https://github.com/lodash/lodash/issues/5261) – Adrian Kaluzinski Oct 18 '21 at 13:08
0

You could potentially use ES6 Template literals to interpolate your strings. I'm going to make some basic assumptions (and hard-code them for this example) in order to only answer the question you asked: your namelist vars are stand-ins for some object parsed from JSON data, you know its structure, and you know how to iterate over a data array to make this solution real world useful.

Template literals are enclosed with backticks "`" instead of quotes, and you surround your variables with ${/* reference variable */}

const namelist1= [{school:'GOVSchool',babydet: { name: 'shanker' }}];
const namelist2= [{school:'GOVSchool',dept:'CS',babydet: { name: 'shanker',section:{sectname:'A Section'}}}];

let Template1 = `Welcome to the ${namelist1[0].school} baby ${namelist1[0].babydet.name}`;
let Template2 = `Welcome to the school ${namelist2[0].school} department ${namelist2[0].dept} babydetails ${namelist2[0].babydet.name} ${namelist2[0].babydet.section.sectname}`;
D Lowther
  • 1,609
  • 1
  • 9
  • 16
0

You can this library for string interpolation. https://www.npmjs.com/package/string-format.String::format is a small JavaScript library for formatting strings, based on Python's str.format().

Remario
  • 3,813
  • 2
  • 18
  • 25
0

I was doing something similar, the only difference is that my JSON object would not have nested properties.

I am using a simple approach (hopefully not too simplistic). It can accept any number of properties to be substituted in the HTML template.

Given a template:

let template =
  '<div><span>{{ text }}</span><span>{{description}}</span></div>';

And some data:

let data = { text: 'My Text', description: 'my description' };

I would get:

<div><span>My Text</span><span>my description</span></div>

See the implementation in JSBin here: https://jsbin.com/zelogof/7/edit?js,console

Better suggestions also welcome!

rodu
  • 2,308
  • 3
  • 15
  • 6