Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smarty JSON Plugin (PHP)
<?php
/**
* Smarty {json} plugin
*
* Type: function
* Name: json
* Purpose: fetch json file and assign result as a template variable (array)
* @author Tony Landis
* @copyright 2007 Tony Landis
* @website http://www.tonylandis.com
* @license GPL License
* @param url (url to fetch)
* @param assign (element to assign to)
* @return array|null if the assign parameter is passed, Smarty assigns the
* result to a template variable
*/
function smarty_function_json($params, &$smarty)
{
if (empty($params['url'])) {
$smarty->_trigger_fatal_error("[json] parameter 'url' cannot be empty");
return;
}
$content = array();
$data = file_get_contents($params['url']);
if(empty($data)) return false;
if(!is_callable('json_decode')) {
require_once 'JSON.php';
$json = new Services_JSON();
$content = $json->decode( trim(file_get_contents($params['url'])) );
} else {
$content = json_decode(trim(file_get_contents($params['url'])));
}
if($params['debug']===true) {
echo "<pre>";
print_r($content);
echo "</pre>";
}
if (!empty($params['assign'])) {
$smarty->assign($params['assign'],$content);
} else {
return $content;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.