jnordberg / wintersmith

A flexible static site generator
http://wintersmith.io/
MIT License
3.5k stars 332 forks source link

Template plugin render function never gets called #283

Closed ghost closed 9 years ago

ghost commented 9 years ago

I am basically trying to use wintersmith-nunjucks and I observed that the fromFile class method gets called (and succeeds) but the render instance method never gets called.

Can someone please advise?

Here is the very simple plugin code and my configuration file below that. My assets and contents directories are empty and I have one file in templates called index.html which is a very basic nunjucks template.

'use strict';

var nunjucks = require("nunjucks");
var path = require("path");

module.exports = function(env, callback) {
  // Load the new nunjucks environment.
  var nenv = new nunjucks.Environment(new nunjucks.FileSystemLoader(env.templatesPath));
  var filterName, filters;

  // Load the filters
  if(env.config.nunjucks) {
    if(env.config.nunjucks.filterdir) {
      env.config.nunjucks.filters.map( function (name) {
        var file = path.join(env.config.nunjucks.filterdir, name + ".js");
        var filter = env.loadModule(env.resolvePath(file), true);
        nenv.addFilter(name, filter);
      });
    }
    if(env.config.nunjucks.filterfile) {
      filters = env.loadModule(env.resolvePath(env.config.nunjucks.filterfile), true);
      for (filterName in filters)
        nenv.addFilter(filterName, filters[filterName]);
    }
  }

  var NunjucksTemplate = function(template) {
    this.template = template;
  };

  NunjucksTemplate.prototype.render = function render(locals, cb) {
    try {
      cb(null, new Buffer(this.template.render(locals)));
    } catch (error) {
      cb(error);
    }
  };

  NunjucksTemplate.fromFile = function fromFile(filepath, cb) {
    var tpl = nenv.getTemplate(filepath.full);
    var ntpl = new NunjucksTemplate(tpl);
    cb(null, ntpl);
  };

  env.registerTemplatePlugin("**/*.*(html|nunjucks)", NunjucksTemplate);
  callback();
};
{
  "locals": {
    "url": "http://localhost:8080",
    "name": "my-site",
    "description": "a site",
    "owner": "the owner",
    "dateDescriptionFormat": "dddd, MMMM Do YYYY",
    "root": "/",
    "assets": "/assets",
    "robotsDisallowed": "*"
  },
  "plugins": [
    "wintersmith-nunjucka"
  ],
  "nunjucks": {
    "filterfile": "nunjucks.filters.js"
  }
}

(wintersmith-nunjucka is my own fork of wintersmith-nunjucks with some very minor changes.)

ghost commented 9 years ago

I stripped down the blog example that wintersmith creates with the command wintersmith new <project-name> and it seems like nothing works at all when you remove plugins/paginator.coffee.

I basically just want wintersmith to stitch together my nunjucks files for me. Do I need to write a plugin to do that or can it just do this out of the box?

Alternatively, where can I read about how to setup the most basic wintersmith site which is not a blog?

ghost commented 9 years ago

So, after hours of hacking I think I figured out that a plugin like paginator.coffee is basically required if you want wintersmith to render any type of template files.

I wish there were some way to make this obvious.

jnordberg commented 9 years ago

That plugin is not required, that's the point. See wintersmith new --template basic You need a entry point for the template to render, a markdown file for example. If you want to generate files without an entry point you need to register a template plugin.