Last Updated: February 25, 2016
·
762
· chrishale

Using PHP in Jeykll

I use Jekyll for a blog at work and love the simplicity it provides in creating static html files. But I recently needed to have a block of PHP code executed at run time (when a user requests the page). Since we host our Jeykll blog on a LAMP stack I justed added a .htaccess file with:

AddType application/x-httpd-php .html .htm

Then put all the PHP that is needed in the html templates. It's a little dirty but works a treat!

Example:

In my _layouts/default.html:

<?php if(!isset($_GET['ajax'])): ?>
<!DOCTYPE html>
<html>
<head>
   <title>{{ page.title }}</title>
   ...
</head>
<body>
<?php endif; ?>

{{ content }}

<?php if(!isset($_GET['ajax'])): ?>
</body>
</html>
<?php endif; ?>

Simples!

1 Response
Add your response

Note: this obviously won't work on Github pages.

over 1 year ago ·