2013-04-16 10:26

Here's a very big gun, there's your foot: PHP support in Nikola

I am a very big proponent of static site generators. I would not have bothered writing Nikola otherwise. But there is always that feeling that maybe there is some little thing which is hard to implement, like a contact form.

And let's face it, the easiest way to solve some of those things is by sticking a few lines of PHP in your HTML.

So, if you really want to, you can do it. I think Nikola (github master) is the first static site generator that supports php code. Here's how:

  1. Add php to your page_compilers (because I will never put it there by default):

    post_compilers = {
        "rest": ('.txt', '.rst'),
        "markdown": ('.md', '.mdown', '.markdown'),
        "textile": ('.textile',),
        "txt2tags": ('.t2t',),
        "bbcode": ('.bb',),
        "wiki": ('.wiki',),
        "ipynb": ('.ipynb',),
        "html": ('.html', '.htm'),
        "php": ('.php'),
  2. Add php posts or pages to your post_pages:

    post_pages = (
        ("posts/*.txt", "posts", "post.tmpl", True),
        ("posts/*.php", "posts", "post.tmpl", True),
        ("stories/*.txt", "stories", "story.tmpl", False),
        ("stories/*.php", "stories", "story.tmpl", False),
  3. Create a php post:

    nikola new_post posts/foo.php
  4. Put php in there:

    .. date: 2013/04/16 09:57:09
    .. title: php test
    .. slug: foo
    Print "Hello, World!";

Build the site as usual, and you should end up with a page with PHP extension, that has that PHP in the "content" area, so it will follow your site's theme. Of course you can't do things like add HTTP headers and such, but hey, read the title.


Comments powered by Disqus

Contents © 2000-2019 Roberto Alsina