carboneio / carbone

Fast and simple report generator, from JSON to pdf, xslx, docx, odt...
https://carbone.io
Other
1.3k stars 190 forks source link

[Feature Request]: "Object type" resolution #186

Open danibs opened 1 year ago

danibs commented 1 year ago

Hi Suppose to have:

{
  "descr",
  "myobj": {
    "a": "aa1",
    "b": "bb1"
  },
  "source": {
    "type": "t1",
    "myobj": {
      "a": "aa2",
      "b": "bb2"
    }
  }
  "dest": {
    "type": "t2",
    "myobj": {
      "a": "aa3",
      "b": "bb3"
    }
  },
  "arr": [
    {
      "t": "ttt",
      "myobj": {
        "a": "aa4",
        "b": "bb4"
      }
    },
    {
      "t": "yyyy",
      "myobj": {
        "a": "aa5",
        "b": "bb5"
      }
    }
  ]
}

as you can see myobj is present in different places. For all places I must repeate placeholders (in my template): {d.myobj.a}-{d.myobj.b} {d.sourse.myobj.a}-{d.sourse.myobj.b} {d.dest.myobj.a}-{d.dest.myobj.b} ...

If what I want to print is always: {*.a}-{*.b}

it might be useful to have some sort of "function": f:myfunc(elem)

{f.myfunc(d.myobj) {f.myfunc(d.souce.myobj)

steevepay commented 1 year ago

Hello, did you try using alias? Documentation: https://carbone.io/documentation.html#alias

danibs commented 1 year ago

With the case I proposed, using alias I must define N "identical" alias:

  1. {#A1 = d.myojb}
  2. {#A2 = d.sourse.myobj}
  3. {#A3 = d.dest.myobj}
  4. case for array

and then use it repeating what I want: {$A1.a}-{$A1.b} {$A2.a}-{$A2.b} {$A3.a}-{$A3.b}

What I mean is a kind of function to which I pass the path to myobj and that function compose the text I need. What do you think about?

It could be that it is only needed for very specific cases, so you don't need to implement it. It's just an idea :smile:

danibs commented 1 year ago

Also, right now alias is not so powerfull. I'm building an example where I have an array and in a cell I need to show a concatenation of two fields (i.e. "a vs b") if another field "c" is "true", but if "d" is "true" then I must show "e". Doing that in a kind of function (or a super-power alias 😉) can help when you must modify the document template... 😅