3 ms·
I was doing something similar to this article (feeding json directly to JS functions) with PHP. I think this is better for the browser. server side, it's heavi
by jhaglund 15y ago
I was doing something similar to this article (feeding json directly to JS functions) with PHP. I think this is better for the browser. server side, it's heavier:
<?php
//inject json
//this puts the json in a div
//then the js gets the div's innerText and parses it as JSON
//so there's an object in js (global) with all the JSON data, with jsonInject's key name
if(isset($jsonInject) && count($jsonInject) > 0){
foreach($jsonInject as $i=>$json){
echo "<div style='display:none;' id='jsonInject_{$i}'>{$json}</div>"
echo "<script>var {$i} = JSON.parse( $('#jsonInject_{$i}')[0].innerText ); </script>";
}
}
?>
fill the associative $jsonInject array with json strings in your controller. then this goes in your templater. it makes a hidden div, puts the json string in there. then jquery pulls the innerText value, it gets parsed as json and stored in a js var. depending on use, i'll sometimes just pass the results of JSON.parse to an initialize method.
I like the proxy caching. I think apache's mod_proxy and mod_cache could do this but with more config (and resources per request).