dynamic-template icon indicating copy to clipboard operation
dynamic-template copied to clipboard

Interpolate nested object properties

Open alex-y-su opened this issue 6 years ago • 6 comments

It is more a feature request, but ES6 string templates can interpolate the nested properties of an object:

describe('String interpolation', () => {
  it("Allow top level fields ", async () => {
    const res = fillTemplate("A: ${a}, B: ${b}", { a: 100, b: "xxx" })
    expect(res).eq("A: 100, B: xxx")
  })

  it("Allow object fields ES6 dynamic", async () => {
    const res = fillTemplate("A: ${model.a}, B: ${model.b}", { model: { a: 100, b: "xxx" } })
    expect(res).eq("A: 100, B: xxx")
  })

  it("Allow object fields ES6", async () => {
    const obj = { model: { a: 100, b: "xxx" } }
    const res = `A: ${obj.model.a}, B: ${obj.model.b}`
    expect(res).eq("A: 100, B: xxx")
  })
})

This package implements it, but the syntax is questioning: https://www.npmjs.com/package/string-interpolation

alex-y-su avatar Jul 16 '19 06:07 alex-y-su

Want to see if this works on version 1 (which actually uses ES6 template strings)?

I might fork this into two separate projects. One for people who need real ES6 templates and know how to sanitise user input, one for people who don't and want a safe version and like complaining on the internet.

mikemaccana avatar Jul 16 '19 08:07 mikemaccana

PS. we use the TDD interface: https://github.com/mikemaccana/dynamic-template/blob/master/test/mocha.opts

mikemaccana avatar Jul 16 '19 08:07 mikemaccana

+1 to this request. I also need the ability to parse nested objects :) Btw, thanks a lot for sharing this work! Save me some time from having to implement it myself!

Want to see if this works on version 1 (which actually uses ES6 template strings)?

I might fork this into two separate projects. One for people who need real ES6 templates and know how to sanitise user input, one for people who don't and want a safe version and like complaining on the internet.

I just confirmed that parsing nested objects worked fine with version 1.0.5! So for now, I'm sticking with it! :)

dharijanto avatar Dec 27 '19 06:12 dharijanto

I didn't realize that version 2 broke that capability, and thus broke my code when I updated to version 2. I am reverting back to 1.0.5. I need real ES6 templates

ThreeDfish avatar Jun 30 '20 17:06 ThreeDfish

You should consider supporting nested objects... perhaps you can include the code shown here: https://stackoverflow.com/a/52711755/10648366

ThreeDfish avatar Jun 30 '20 18:06 ThreeDfish

Hey all. Using real ES6 StringTemplates is exactly what this module did in the first place: https://github.com/mikemaccana/dynamic-template/commit/166dcc048a868290824f369db188603bc7684ae3

Basically a whole bunch of fucking assholes brigaded me into changing it because "WHAT IF PEOPLE USING YOUR MODULE ACCEPT UNSANITISED INPUT". Which is fucking stupid: don't accept unsanitised input from users.

Maybe I should revert that commit with an explicit message to go fuck themselves.

Pardon the language. I'm not angry at anyone in this thread. I'm angry at letting myself be bullied into removing something that I and many others liked.

Someone else: revert that commit, bump the major version, and send me a PR.

mikemaccana avatar Jul 01 '20 10:07 mikemaccana