From 8f4ed5202870161e24cb9e6c91039158d3e902bf Mon Sep 17 00:00:00 2001
From: Florent Bruneau
Date: Sat, 24 Sep 2011 09:33:54 +0200
Subject: [PATCH] Add basic support for a json output.
If banana get output=json in its parameters, it will produce a json
output that can be used in order to update the page in javascript.
This works well with the box list, I didn't even test it on other pages.
Signed-off-by: Florent Bruneau
---
banana/banana.inc.php.in | 2 +-
banana/page.inc.php | 20 +++++++++++++++++++-
banana/templates/banana-boxlist.inc.tpl | 2 +-
examples/index.php | 5 +++++
4 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in
index d28cbbc..b4878a0 100644
--- a/banana/banana.inc.php.in
+++ b/banana/banana.inc.php.in
@@ -184,7 +184,7 @@ class Banana
if ($pageclass == 'BananaPage') {
Banana::load('page');
}
- Banana::$page = new $pageclass;
+ Banana::$page = new $pageclass(@$this->params['output']);
$types = array('multipart/report' => _b_('Rapport d\'erreur'),
'multipart/mixed' => _b_('Composition'),
'text/html' => _b_('Texte formaté'),
diff --git a/banana/page.inc.php b/banana/page.inc.php
index 8b2602c..1d62313 100644
--- a/banana/page.inc.php
+++ b/banana/page.inc.php
@@ -20,12 +20,16 @@ class BananaPage extends Smarty
protected $killed = array();
protected $actions = array();
+ protected $mode;
+ protected $json_params = array();
+
public $css = '';
- public function __construct()
+ public function __construct($mode)
{
parent::Smarty();
+ $this->mode = strtolower($mode);
$this->compile_check = Banana::$debug_smarty;
$this->template_dir = dirname(__FILE__) . '/templates/';
$this->compile_dir = Banana::$spool_root . '/templates_c/';
@@ -63,6 +67,17 @@ class BananaPage extends Smarty
return true;
}
+ /** Assign a variable in the page.
+ */
+ public function assign($var, $value)
+ {
+ if ($this->mode === 'json') {
+ $this->json_params[$var] = $value;
+ } else {
+ parent::assign($var, $value);
+ }
+ }
+
/** Register an action to show on banana page
* @param action_code HTML code of the action
* @param pages ARRAY pages where to show the action (null == every pages)
@@ -148,6 +163,9 @@ class BananaPage extends Smarty
*/
public function run()
{
+ if ($this->mode === 'json') {
+ return json_encode($this->json_params);
+ }
$tpl = $this->prepare();
if (!isset($this->pages[$this->page])) {
$this->trig(_b_('La page demandée n\'existe pas'));
diff --git a/banana/templates/banana-boxlist.inc.tpl b/banana/templates/banana-boxlist.inc.tpl
index 102bf07..38ab181 100644
--- a/banana/templates/banana-boxlist.inc.tpl
+++ b/banana/templates/banana-boxlist.inc.tpl
@@ -5,7 +5,7 @@
{/if}
-
+
{if $withsubs}
diff --git a/examples/index.php b/examples/index.php
index 4419ee3..50e7d5a 100644
--- a/examples/index.php
+++ b/examples/index.php
@@ -126,6 +126,11 @@ $feed = $banana->feed(); // Get a link to banana's feed. You need to use Ba
$bt = $banana->backtrace(); // Get protocole execution backtrace
session_write_close();
+if (@strtolower($_GET['output']) === 'json') {
+ header('Content-Type: text/javascript');
+ echo $res;
+ exit;
+}
// Genererate the page
?>
--
2.1.4