Create a engine to replace template entries with json objects in python

Viewed 2652

I want to create an engine in python that can replace tags in a template file with objects from json? I've loooked at python based engines that use regex but they are overly complicated and I am a little confused as to how to start an go about it. Any code to start would help

Sample json file

{
  "webbpage": {
    "title": "Stackoverflow"
  },
  "Songs": {
    "name": "Mr Crowley"
  },
  "CoverArtists": [
    { "name": "Ozzy", "nicknames": ["Ozzman","Ozzster"] },
    { "name": "Slipknot", "nicknames": ["Slip"] }
  ]
}

Sample template file

<html>
  <head>
    <title><%% webbpage.title %%></title>
  </head>
  <body>
    <h1><%% Songs.name %%></h1>
    <%% EACH CoverArtists artist %%>
      <%% artist.name %%>
      <%% EACH CoverArtists.nicknames nickname %%>
        <p><%% nickname %%></p>
      <%% END %%>
    <%% END %%>
  </body>
</html>

Basically variables are identified between <%% and %%> and loops are identifed between <%% EACH .. %%> AND <%% END %%> and basically output html

2 Answers
Related