<?php // Include json template require_once 'jsontemplate.php'; // Templates and page data $templatePath = '/var/www/robinwinslow.co.uk/jsontemplates'; $wrapperFile = 'wrapper.json.html'; $pagesData = array('index' => array('file' => 'index.json.html', 'title' => 'Home'), 'academic' => array('file' => 'academic.json.html', 'title' => 'Academic'), 'controller' => array('file' => 'controller.json.html', 'title' => 'Controller'), 'me' => array('file' => 'me.json.html', 'title' => 'Me'), 'portfolio' => array('file' => 'portfolio.json.html', 'title' => 'Portfolio'), 'professional' => array('file' => 'professional.json.html', 'title' => 'Professional'), 'technical' => array('file' => 'index.json.html', 'title' => 'Technical'), '404' => array('file' => 'error.json.html', 'status' => '404', 'title' => 'Page not found', 'heading' => 'Page not found', 'paragraphs' => array('Huh?', 'We can\'t find that page'))); // Get this page data $page = empty($_GET['page']) ? 'index' : $_GET['page']; if (!array_key_exists($page, $pagesData)) { $page = '404'; } $pageData = $pagesData[$page]; // Create json templates $contentTemplate = JsonTemplateModule::FromFile($templatePath . '/' . $pageData['file']); $wrapperTemplate = JsonTemplateModule::FromFile($templatePath . '/' . $wrapperFile); $pageData['content'] = $contentTemplate->expand($pageData); // Expand out the wrapper header("HTTP/1.1 200 OK"); header('Content-type: text/html'); echo $wrapperTemplate->expand($pageData);
function render($data, $callback = null) { if (is_string($data)) { $data = json_decode($data); } $JM = JsonTemplateModule::pointer(); return $JM->Execute($this->program->Statements(), new JsonTemplateScopedContext($data, $this->compile_options['undefined_str']), $callback); }
function render($data, $callback = null) { if (is_string($data)) { $data = json_decode($data); } return JsonTemplateModule::pointer()->Execute($this->program->Statements(), new JsonTemplateScopedContext($data), $callback); }
#!/usr/bin/env php <?php require dirname(__FILE__) . "/jsontemplate.php"; if (count($argv) <= 1) { print "usage: " . $argv[0] . " 'Hello [var]!' '{\"var\":\"World\"}' '{\"meta\":\"[]\"}'\n"; } if (isset($argv[2])) { $data = json_decode($argv[2]); } else { $data = array(); } if (isset($argv[3])) { $options = json_decode($argv[3]); } else { $options = array(); } try { print JsonTemplateModule::expand($argv[1], $data, $options); } catch (Exception $e) { $class = preg_replace('/^JsonTemplate/', '', get_class($e)); print "EXCEPTION: " . $class . ": " . $e->getMessage() . "\n"; }
function testNestedRepeatedSections() { $t = "\n[header]\n---------\n[.section people]\n[.repeated section @]\n [name]: [.repeated section attributes][@] [.end]\n[.end][.end]"; $d = array('header' => 'People', 'people' => array(array('name' => 'Andy', 'attributes' => array('jerk', 'cool')), array('name' => 'Bob', 'attributes' => array('nice', 'mean', 'fun')))); $e = "\nPeople\n---------\n Andy: jerk cool \n Bob: nice mean fun \n"; $o = array('meta' => '[]'); $this->assertEquals(JsonTemplateModule::expand($t, $d, $o), $e); }