mratmsf / json-template

Automatically exported from code.google.com/p/json-template
0 stars 0 forks source link

Use @. to stop scope chain lookup #52

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago

A template string ...

{# This is a comment and will be removed from the output.}

{.section songs}
  <h2>Songs in '{playlist-name}'</h2>

  <table width="100%">
  {.repeated section @}
    <tr>
      <td><a href="{url-base|htmltag}{url|htmltag}">Play</a>
      <td><i>{title}</i></td>
      <td>{artist}</td>
    </tr>
  {.end}
  </table>
{.or}
  <p><em>(No page content matches)</em></p>
{.end}

... combined with a data dictionary ...

{
  "url": "happy!",
  "url-base": "http://example.com/music/", 
  "playlist-name": "Epic Playlist", 
  "songs": [
    {
      "artist": "Grayceon", 
      "title": "Sounds Like Thunder"
    }, 
    {
      "url": "2.mp3", 
      "artist": "Thou", 
      "title": "Their Hooves Carve Craters in the Earth"
    }
  ]
}

... gives output:

  <h2>Songs in 'Epic Playlist'</h2>

  <table width="100%">
    <tr>
      <td><a href="http://example.com/music/happy!">Play</a>
      <td><i>Sounds Like Thunder</i></td>
      <td>Grayceon</td>
    </tr>
    <tr>
      <td><a href="http://example.com/music/2.mp3">Play</a>
      <td><i>Their Hooves Carve Craters in the Earth</i></td>
      <td>Thou</td>
    </tr>
  </table>

But in the template, if this line:

      <td><a href="{url-base|htmltag}{url|htmltag}">Play</a>

were changed to:

      <td><a href="{url-base|htmltag}{@.url|htmltag}">Play</a>

It would return:

      <td><a href="http://example.com/music/">Play</a>

instead of :

      <td><a href="http://example.com/music/happy!">Play</a>

A complete nonsensical example, but illustrates what I mean I hope!

Original issue reported on code.google.com by sroussey on 18 Feb 2010 at 2:27

GoogleCodeExporter commented 8 years ago
I see.  Is there a case where you need this?

I think it basically makes sense.

Original comment by gtempacc...@yahoo.com on 18 Feb 2010 at 6:28

GoogleCodeExporter commented 8 years ago
Yes, I do need it to stop some scenarios from occurring. As I have a system for
includes, and a system for doing lookups into the parent chains of includes and 
their
scopes, this starts to happen more often. (I do this by a custom formatter and a
custom undefined_str function). So, in such a big system, it happens.

(There is a corollary: ^.xxx to, say, look up in reverse order from the top, 
but I
think that introduces something far more new in concept and implementation.)

Original comment by sroussey on 18 Feb 2010 at 4:31