dimanche 3 juillet 2016

PHP template system vs javascript AJAX template


My PHP template looks like this:

$html=file_get_contents("/path/to/file.html");
$replace=array(
"{title}"=>"Title of my webpage",
"{other}"=>"Other information",
...
);
foreach(replace AS $search=>$replace){
    $html=str_replace($search,$replace,$html);
}
echo $html;

I am considering switching to a javascript/ajax template system. The AJAX will fetch the $replace array in JSON format and then I'll use javascript to replace the HTML.

The page would then be a plain .html file and a loading screen would be shown until the ajax was complete.

Is there any real advantages to this or is the transition a waste of time?

A few of the reasons I think this will be beneficial:

  • Page will still load even if the Mysql or PHP services are down. If the ajax fails I can handle it with an error message.
  • Bot traffic (and anything else that doesnt run JS) will cause very little load to my server since the ajax will never be sent.

Please let me know what your thoughts are.


Aucun commentaire:

Enregistrer un commentaire