{"id":8,"date":"2012-05-23T15:45:03","date_gmt":"2012-05-23T15:45:03","guid":{"rendered":"http:\/\/www.moderncentral.com\/blog\/?p=8"},"modified":"2012-09-20T09:53:44","modified_gmt":"2012-09-20T09:53:44","slug":"making-php-and-shtml-play-nice","status":"publish","type":"post","link":"http:\/\/www.moderncentral.com\/blog\/?p=8","title":{"rendered":"Making PHP and SHTML Play Nice"},"content":{"rendered":"<div>The\u00a0css\u00a0style and much of the layout of this site was stolen wholesale from http:\/\/www.freecsstemplates.org\/. Laziness won out, I didn&#8217;t feel like designing the site from the ground up at the time, and these files, written using\u00a0SHTML\u00a0server-side includes, provided an easy spring-board. Now, as I try to expand the site several months later, I find myself locked into a format for which I had not thought out all of the ramifications.<\/div>\n<p>&nbsp;<\/p>\n<div><\/div>\n<div>So first, a word about the site implementation:\u00a0SHTML\u00a0provides an exceedingly simple server-side scripting language on top of HTML. Essentially, it gives you server-side includes (the ability to insert code from a different file before serving content to the user, which I use to display the same side-bar, footer, etc&#8230; on all of this site&#8217;s pages), simple loops and control directives, and not much else. This is brilliant if you&#8217;re interested in maintaining a site with shared content across a simple structure.<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>Now,\u00a0wordpress, the popular content management system serving the text you are reading right now, is built on\u00a0PHP.\u00a0PHP\u00a0is also a server-side scripting language, but orders of magnitude more complex (and powerful) than SHTML. Naively, to\u00a0 integrate\u00a0wordpress\u00a0would require migrating all of my\u00a0SHTML\u00a0files to\u00a0PHP. This is not an excruciatingly complicated process: rename a few files, change a few <code><\/code><\/div>\n<div><code>&lt;!-- #include virtual=\"myfile.html\" --&gt;<\/code><\/div>\n<div>into<\/div>\n<div><code>&lt;?php\u00a0include 'myfile.html' ?&gt;<\/code>,<\/div>\n<div>and voila, the site would be ready to use\u00a0wordpress. But, I&#8217;m ornery, and I don&#8217;t like change, and more importantly, I wanted to understand if the two languages could be made to run side by side.<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>Short answer: no, they can&#8217;t. The processors which parse the\u00a0SHTML\u00a0or PHP\u00a0code and generate HTML to push to the client are chosen based on the\u00a0filename, and you cannot\u00a0 modify the server behavior to run these processors serially.<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>Hackish answer: yes, they can! The very feature of\u00a0SHTML\u00a0which originally motivated me to use it, the server-side includes, can solve this problem too. The\u00a0SHTML\u00a0include\u00a0 directive does not simply dump text into the page, in the case of a recognized script, the server builds the result of the script before including it.\u00a0Practically speaking, that means you can execute multiple\u00a0PHP,\u00a0perl, or any other\u00a0CGI scripts, in the same file. The query string, which\u00a0wordpress\u00a0uses to determine which posts to display, can also be passed to the script by using some more server-side magic:<\/div>\n<div><code>&lt;!--#set var=\"blog_script\" value=\"blog.php?$QUERY_STRING\" --&gt;<\/code><\/div>\n<div><code>&lt;!--#include virtual=\"$blog_script\" --&gt;<\/code><\/div>\n<p>&nbsp;<\/p>\n<div>The may seem slightly round-about, but as it turns out, it incurs little to no penalty in the rendering time for the page. In my case, a few extra files (blog.php,\u00a0permalink.php) with snippets of\u00a0PHP\u00a0allow me to render entries written in\u00a0wordpress\u00a0without any other site<\/div>\n<div>redesign.<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>Conclusion: it would have been faster to migrate to php, but now I know a bit more about the technology serving this site. Active laziness wins again.<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>P.S.<\/div>\n<div><\/div>\n<div>I said that the performance of pages served using PHP and SHTML were essentially the same. This is perhaps not quite so true.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.moderncentral.com\/blog\/wp-content\/uploads\/2012\/05\/ssi_timing1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-34 aligncenter\" title=\"\" src=\"http:\/\/www.moderncentral.com\/blog\/wp-content\/uploads\/2012\/05\/ssi_timing1-300x203.png\" alt=\"\" width=\"300\" height=\"203\" \/><\/a><\/p>\n<p>The above plot shows the retrieval time for two identical pages, one using SHTML to include a PHP snippet, the other using PHP natively. You can see that the SHTML page is slower on average (by about 12% actually), but that the spread easily covers this difference.<\/p>\n<p>Yes, this means I went through the effort of migrating one of my pages from SHTML to PHP, but that&#8217;s besides the point. \ud83d\ude09<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0css\u00a0style and much of the layout of this site was stolen wholesale from http:\/\/www.freecsstemplates.org\/. Laziness won out, I didn&#8217;t feel like designing the site from the ground up at the time, and these files, written using\u00a0SHTML\u00a0server-side includes, provided an easy &hellip; <a href=\"http:\/\/www.moderncentral.com\/blog\/?p=8\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":10,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":13,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/13"}],"wp:attachment":[{"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moderncentral.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}