Skip to main content

Ralsina.Me — Roberto Alsina's website

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

I am a very big pro­po­nent of stat­ic site gen­er­a­tors. I would not have both­ered writ­ing Niko­la oth­er­wise. But there is al­ways that feel­ing that maybe there is some lit­tle thing which is hard to im­ple­men­t, like a con­tact for­m.

And let's face it, the eas­i­est way to solve some of those things is by stick­ing a few lines of PHP in your HTM­L.

So, if you re­al­ly want to, you can do it. I think Niko­la (github mas­ter) is the first stat­ic site gen­er­a­tor that sup­ports php code. Here's how:

  1. Add php to your page_­com­pil­ers (be­cause I will nev­er put it there by de­fault­):

    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. Cre­ate 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
    -->
    
    <?php
    Print "Hello, World!";
    ?>

Build the site as usu­al, and you should end up with a page with PHP ex­ten­sion, that has that PHP in the "con­tent" area, so it will fol­low your site's theme. Of course you can't do things like add HTTP head­ers and such, but hey, read the ti­tle.

James Mills / 2013-04-16 23:56:

Holy crap!!! *BOOM!!!*


Contents © 2000-2024 Roberto Alsina