Search code examples
phphtmltemplateskohana

Best way to use a server side language for development, but deploy to static HTML


We have a client for whom we build a lot of template based sites. Ideally we would use something like kohana (http://www.kohanaphp.com/) to handle the templating and make doing site wide changes a breeze.

Unfortunately our client cannot (and will not) host any server side code (and before you ask, this will not change and hosting the files ourselves is not an option), so any files deployed to them must be HTML, Javascript, CSS, images and Flash only.

Is there a good way to develop in a framework environment like kohana to make the site manageable, but be able to deploy or export a HTML only version of the site (there is no dynamic aspect to the site such as searching that requires server side languages, and no database use)?

I guess it would be similar to spidering the site, but I'd like something a bit more reliable because some page assets are loaded dynamically with javascript.

Thanks


Solution

  • I use Template Toolkit (Perl) and have a simple script that generates static files from the templates. This is great for the situation you are in (common navigation etc etc).

    It comes with a ttree command that'll process a directory tree and put the results in another.

    Here's the tt.rc file I use:

    # ignore these files (regular expressions)
    ignore = \.svn
    ignore = ^#
    ignore = ~$
    ignore = .DS_Store
    ignore = \.tt$
    
    # if these template files change, reprocess everything
    depend *=tpl/wrapper,tpl/defaults,style/default.html
    
    # just copy these files, don't process as templates
    copy = \.(gif|png|pdf|jpg)$
    
    # verbose output
    verbose
    
    # recurse into subdirectories
    recurse
    
    # setup some defaults from tpl/defaults
    pre_process = tpl/defaults
    
    # process this file instead of the real file (see below how this is used)
    process     = tpl/wrapper
    
    # process files from src/, output to html/
    # extra templates in lib/ (tpl/wrapper for example).
    src  = src
    dest = html
    lib  = lib
    

    A couple of special files, tpl/defaults is

    [%-  page = {
                title = template.title,
                style = template.style or 'default.html'
        };
    
        base = INCLUDE tpl/base_uri;
    
        # don't include any whitespace from here...
        RETURN;
    -%]
    

    And tpl/wrapper is

    [%- content = PROCESS $template;    
       IF !template.name.search('.html') OR page.style == 'none';
          content;
       ELSE;
          default_style_template = "style/" _ page.style;
          PROCESS $default_style_template;
       END;
    %]
    

    This will process the real template; put the results in the content variable and then process the style template (set with page.style in tpl/defaults; defaults to defaults.html).

    the lib/style/default.html style file just needs to have

    [% content %]
    

    somewhere to include the real template; before and after that you can have the standard footer and headers.

    You can read more about Template Toolkit at tt2.org.

    Another option would be to use wget (or similar) in recursive mode to "mirror" pages generated by PHP on the development server; but I wouldn't recommend that.