0

I have a template file that contains standard text, that I would like users the ability to "customize" it using settings defined elsewhere in JSON files. A typical settings file looks something like:

user.json

{
"name": {
    "first": "John",
    "last": "Doe"
},
"address": {
    "addr1": "123 South St",
    "city": "Citiville",
    "geo": {
        "lat": "123.123123",
        "long": "80.123123"
    }
}

} The way the templating works is that custom variables can be added by using a format that we define and is inserted into the file by the use of pre-defined buttons adding, for example the LAT and LONG:

  1. {{
  2. NameOfJsonFileInUpperCaseWithoutExtension
  3. _
  4. ListOfArrayElementsInOrderDenotedByHyphens
  5. }}

So, after placing the cursor in the template file where you want to add your LATITUDE, pressing the respective button on the toolbar adds something like the following in the template file

You can find us using {{USER_ADDRESS-GEO-LAT}}

would get parsed as required to read user.json (from a pre-defined folder) into an array and then using the ['address']['geo']['lat'] elements of that array to get the actual data being requested and so when the template file is actually presented to the user, it reads:

You can find us using 123.123123

I've gotten so far as to get the elements as a string, and the file contents json_decoded into an array but I can't seem to figure out how to denote an array's elements with a variable.

$elements is currently (and literally, including quotes and square brackets)

['address']['geo']['lat'] 

and $contentArray is simply the file contents as an array

json_decode(file_get_contents('user.json', true));

But, I can't work out how to get the actual data - I thought I'd have to use those lovely curly braces but I can't seem to get it...

  1. $contentArray"{$elements}" is the "closest" but I get Array['address']['geo']['lat']
  2. $contentArray[$elements]
  3. ${"contentArray . $elements"}

I'm beginning to think this isn't possible, but thought I'd ask you fine chaps and chappesses out in SO land if I'm barking up completed the wrong tree...?

5
  • fyi. your json string is missing opening closing bracket in address nesting, so basically, its an invalid one Commented Jun 11, 2019 at 0:55
  • or why not use a database, then just fetch over json responses on demand, its just cumbersome trying to read, then write json strings in files, in a database, you'll be able to search much faster Commented Jun 11, 2019 at 0:57
  • Definitely the address key is missing an opening and closing brace, so that would be invalid json. Commented Jun 11, 2019 at 0:57
  • 1
    i think the trouble you're having is having to parse up that string to denote the nesting inside the key arrays. here's your clue by the way stackoverflow.com/questions/27929875/… this should give you some direction on the path parsing. take note keys are case sensitive though, so the templating keys that you provide should match the json key strings inside Commented Jun 11, 2019 at 1:02
  • Thanks to all, wasn't sure who posted the articles at the top but the one that "won" was that with at least 8 answers to it, from Drupal's code : stackoverflow.com/questions/9628176/… Commented Jun 11, 2019 at 1:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.