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"

1
  • 1
    JSON is a textual notation for data exchange. (More.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Those are just objects. Commented Mar 6, 2017 at 3:25

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Please keep in mind that _.template also allows to evaluate JavaScript code which could introduce severe security risk for unescaped input. See possible issue
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}`;

Comments

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().

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.