Created
June 8, 2010 15:32
-
-
Save cowboy/430200 to your computer and use it in GitHub Desktop.
PHP "JSON heredoc" for inline data.. good? bad? comments? I'm trying to keep it simple.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
## Parse query string. | |
$sb_league = $_GET['league'] ? strtolower( $_GET['league'] ) : 'all'; | |
## Nav data, as a JSON heredoc. Much less verbose than PHP arrays! | |
$navs = json_decode(<<<JSON | |
{ | |
"mlb": { | |
"title_link": "http://stats.boston.com/mlb/scoreboard.asp", | |
"nav": [ | |
{ "title": "Full Schedule", "url": "http://stats.boston.com/mlb/teamreports.asp?tm=02&report=schedule" }, | |
{ "title": "Statistics", "url": "http://stats.boston.com/mlb/teamreports.asp?yr=2009&tm=2&btnGo=Go&report=stats" }, | |
{ "title": "Standings", "url": "http://stats.boston.com/mlb/standings.asp" } | |
] | |
}, | |
"nfl": { | |
"title_link": "http://stats.boston.com/fb/scoreboard.asp", | |
"nav": [ | |
{ "title": "Full Schedule", "url": "http://stats.boston.com/fb/teamstats.asp?yr=2009&tm=17&btnGo=Go&type=schedules" }, | |
{ "title": "Statistics", "url": "http://stats.boston.com/fb/teamstats.asp?teamno=17&type=stats" }, | |
{ "title": "Standings", "url": "http://stats.boston.com/fb/totalstandings.asp" }, | |
{ "title": "Season timeline", "url": "http://timelines.boston.com/patriots" } | |
] | |
}, | |
"nhl": { | |
"title_link": "http://stats.boston.com/nhl/scoreboard.asp", | |
"nav": [ | |
{ "title": "Full Schedule", "url": "http://stats.boston.com/nhl/teamstats.asp?teamno=01&btnGo=Go&type=schedule" }, | |
{ "title": "Statistics", "url": "http://stats.boston.com/nhl/teamstats.asp?teamno=01&btnGo=Go&type=stats" }, | |
{ "title": "Standings", "url": "http://stats.boston.com/nhl/standings_conference.asp" }, | |
{ "title": "Season timeline", "url": "http://timelines.boston.com/bruins#/seasons/2009" } | |
] | |
}, | |
"nba": { | |
"title_link": "http://stats.boston.com/nba/scoreboard.asp", | |
"nav": [ | |
{ "title": "Full Schedule", "url": "http://stats.boston.com/nba/teamstats.asp?teamno=02&btnGo=Go&type=schedule" }, | |
{ "title": "Statistics", "url": "http://stats.boston.com/nba/teamstats.asp?teamno=02&btnGo=Go&type=totstats" }, | |
{ "title": "Standings", "url": "http://stats.boston.com/nba/standings_conference.asp" }, | |
{ "title": "Season timeline", "url": "http://timelines.boston.com/celtics" } | |
] | |
} | |
} | |
JSON | |
,true) or die( 'Invalid JSON, please validate at www.jsonlint.com.' ); | |
$nav = $navs[ $sb_league ]; | |
## Output. | |
?><div class="scorebelt"> | |
<div class="scorebelt-nav"> | |
<? if ( $nav['title_link'] ): ?> | |
<h3><a href="<?= $nav['title_link'] ?>">Scoreboard</a></h3> | |
<? else: ?> | |
<h3>Scoreboard</h3> | |
<? endif; ?> | |
<ul> | |
<? foreach ( $nav['nav'] as $item ): ?> | |
<li><a href="<?= $item['url'] ?>"><?= $item['title'] ?></a></li> | |
<? endforeach; ?> | |
</ul> | |
</div> | |
</div> |
voxwerk - thanks for the info. As a primarily JavaScript developer, I only intended this approach to be used as a "quick hack" to manage simple data structures for basic one-off scripts, not as a replacement for anything more robust. And of course, with the corresponding PHP TextMate bundle JSON heredoc syntax highlighting patch, syntax errors shouldn't be an issue!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just remember that no matter how efficiently
json_decode()
performs here, the fact that you're not using native PHP arrays will bypass any opcode caching systems like APC or xcache. You also delay syntax checking to the momentjson_decode()
is called. Just thought I'd throw those thoughts out there. :)