plugin-render

The render plugin enables your application to handle server-side rendering (currently) using nunjucks, extending the default action and intent classes.

Installation and usage
# install the plugin
npm i --save thorin-plugin-render@1.x
# install nunjucks
npm i --save nunjucks@2.4.2

'use strict';
   // app.js entry file
   const thorin = require('thorin');

   thorin.addPlugin(require('thorin-plugin-render'));   // <-- add this line
   thorin.run((err) => {});

# run once to allow the plugin to set itself up
   node app.js --setup=plugin.render
Default configuration
  • enginenunjucksthe rendering engine to use. Currently, only this is supported.
  • pathapp/viewsthe path to your views folder, relative to thorin.root
Plugin functionality
pluginObj.render(template, locals, fn)
Manually call the render function on a template with the given local variables. Once the HTML is generated, callback the function
  • templatestringthe template path, relative to thorin.root
  • localsobjectthe locals variable that can be accessed by the template.
  • fnfunctionthe function to call when rendering is completed or error.
pluginObj.addLocal(key, val)
Extends the global locals object by adding keys to it or merging other objects with it.
  • pluginObj.addLocal({some: 'object'}) merge the given object with the global locals
  • pluginObj.addLocal('aKey', 'aValue') add the given key to the global locals object.
pluginObj.getLocals() : Object
Returns the current global locals object of the plugin.
pluginObj.instance() : Nunjucks
Returns the render instance. This is useful when you want to add custom filters or access other features.
Extended Thorin.Action
actionObj.render(template, when)
The function will enable you to render swig or html templates that have access to rendering utilities and the intent object.
  • templatestringthe template path you wish to use
  • whennumber | stringwhen should the given template be used. By default, it will be used on success requests. You can also render specific templates for generic error requests or specific status codes.
'use strict';
thorin.dispatcher
   .addAction('my.action')
   .alias('GET', '/home')
   .render('home.swig')          // render the home.swig template by default
   .render('error.swig', 500);   // render the error.swig template on 500 errors

// You can also select the template dynamically, based on the incoming intent
thorin.dispatcher
   .addAction('account')
   .alias('GET', '/admin/:type')
   .input({
      type: dispatcher.validate('STRING')
   })
   .render('invalid_type.swig', 400)   // render this when no type is given
   .render('error.swig', 'error')      // when other any kind of error occurs.
   .render((intentObj) => {      // conditional rendering
      let type = intentObj.input('type');
      if(type === 'app') return 'app.swig';
      return 'dashboard.swig';
   });
action event: render
Whenever an action will render the HTML of a template, the render action event will be triggered, therefore you can inject functionality before and after the rendering.
  • actionObj.before('render', fn) calls the function with fn(intentObj), before we begin rendering the template
  • actionObj.after('render', fn) calls the function with fn(intentObj, html), after the HTML output was generated.
Rendering utilities
{{ config(key) }}
Access the application's configuration, essentially translates to thorin.config
{{ asset(path, version) }}
Builds up an asset path that will be used with the config.cdn setting.
  • pathstringthe asset path, relative to the /public folder.
  • versionstring | booleanif specified, add a ?v={version} in the querystrin of the asset. If it is set on true, extract the version from the project's package.json file.
{{ actionAlias(action) }}
Returns the first alias path for the given action or an empty string. This is used to map url's to actions in your views, so that in the event of a path change, you do not have to update your routes.
{{ isAction(action, result) }}
A small utility function that verifies if the given action code is the one of the current request. If it matches, output the result param

<div class="{{ isAction('home.index','active') }}">Home menu item.</div>
thorin
The thorin app reference is also available in templates.
intent
The intent object of a request is also available in templates.
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.