A View engine
npm install view
This simple pattern allows for a very clean View which can easily be extended with any advanced features you might want.
It ships with templating support for HTML or Markdown, but can easily be extended with the templating engine of your choice.
The project is well tested with easy to read test cases. It's been in development for several years and is used in many production applications such as hook.io
The structure of the folder which contains the view maps directly to the constructed View object.
Will map to:
// renders View-PresentermyViewindexpresentopts cb;myViewaboutindexpresentopts cb;myViewaboutteampresentopts cb;myViewcontactpresentopts cb;// also can introspect templates as stringsmyViewindextemplate;myViewaboutindextemplate;myViewaboutteamtemplate;
Presenters are paired with a View by existing in the same directory and sharing the same name as the View template.
View.present() is called, this Presenter method will execute on the context of the View template.
module'exports' =// this.$ is a jQuery like selector with it's HTML context set to the current `View.template`var $ = this$;// you can perform normal jQuery like modifications here$'h1'html'Hello';// once you are done transforming the template, continue with the newly modified html// notice this uses a callback? this means you can retrieve data from any remote source!// simply require the module you need in this Presenter and call out.callbacknull $html;
Our unit tests will give a much better idea on how this works in practice.
A simple layout pattern is available for Views.
If a View has a
layout.js file present, that layout file will yield the results of the View-Presenter into the layout.
If no layout is found at the current level of the View-Presenter tree, the View will recurse upwards until a layout or the root is found.
// Will yield the contents of the "index" View inside the ".yield" elementmyViewindexpresentopts cb;// In the case of a nested view, the layout will still be appliedmyViewaboutteampresentopts cb;myViewaboutindexpresentopts cb;myViewcontactpresentopts cb;
Our unit tests will also give a better idea on how nested layouts will be applied.
view module ships with a built-in Node.js middleware. This middleware will map the View to incoming urls of your web application.
// for simplicity, assume server is an Express.js applicationserveruseviewmiddleview: myView ;
Will now map to the following urls on your server: