Skip to content

Financial-Times/n-express

Folders and files

NameName
Last commit message
Last commit date
Jan 31, 2015
Feb 5, 2015
Feb 5, 2015
Jan 29, 2015
Jan 9, 2015
Dec 15, 2014
Feb 5, 2015
Jan 31, 2015
Feb 4, 2015
Feb 6, 2015

Repository files navigation

next-express

Slightly enhanced Express.

Comes with:-

  • Handlebars (with added support for loading partials from bower_components)
  • Origami Image Service integration
  • Sensible error handling (configurable via environment variables)
  • Full Next Flags integration
  • Anti-search engine GET /robots.txt (possibly might need to change in the future)
  • Promise & (Isomorphic) Fetch polyfills
  • Exposes everything in the app's ./public folder via ./{{name-of-app}}
  • Exposes app name via __name to templates
  • Default layout, which includes all the scripts and styles you'd expect (cuts the mustard, main.css, main.js). Also adds header and footer markup (a vanilla layout without header and footer is also available)
  • Provides NODE_ENV to templates via __environment
  • __isProduction is true if NODE_ENV equals PRODUCTION
  • Provides a range of handlebars helpers, including template inheritance

Installation

npm install --save ft-next-express

When using the default layout there is also a hard dependency on some bower components. To install them (and add to your app's bower.json) run the following on your local machine. It's assumed you will have bower installed globally.

chmod +x path/to/ft-next-express/bower-install.sh

Example app

main.js

var express = require('ft-next-express');

var app = express({

	// Optional.  If name is not provided, next-express will try to infer it from package.json
	name: "xian",

	// Optional
	helpers: {
		uppercase: function(options) {
			return options.fn(this).toUpperCase();
		}
	}
});

app.get('/', function(req, res, next) {
	res.render('main', {
		title: "FT",
		image: "https://avatars0.githubusercontent.com/u/3502508?v=3",
		date: new Date(),
		text : "<p>This wont be shown</p><p>This will be shown</p><p>This wont be shown</p>"
	});
});

app.listen(process.env.PORT, function() {
	console.log("Listening on " + process.env.PORT);
});

views/main.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>{{title}}</title>
	<!-- this will be output as <link rel="stylesheet" href="/xian/main.css"> -->
	<link rel="stylesheet" href="/{{__name}}/main.css">
</head>
<body>
	<h1>{{title}}</h1>
	{{#uppercase}}this text will be uppercase{{/uppercase}}
	<h2>An image resized to 150px wide</h2>
	<img src="{{#resize 150}}{{image}}{{/resize}}" />

	{{#flags.myFlag.isSwitchedOn}}
	The 'myFlag' flag is switched on
	{{/flags.myFlag.isSwitchedOn}}

	<time data-o-component="o-date" class="o-date" datetime="{{#dateformat}}{{date}}{{/dateformat}}">
		{{#dateformat "dddd, d mmmm, yyyy"}}{{date}}{/dateformat}}
	</time>

	{{paragraphs text start=1 end=2}}

	{{#removeImageTags}}
	Image<img src="someimage.jpg" alt="This wont be shown"/>EndImage
	{{/removeImageTags}}
</body>
</html>
## Handlebars inheritance This is achieved by means of two helpers:
  • outputBlock used in the parent template to indicate where content should be output. Can also define default content
  • defineBlock used in the child template to define the desired output to insert into the block
// parent.html
{{#outputBlock 'my-block'}}default content{{/outputBlock}}

// child.html
{{#defineBlock 'my-block'}}
	Mustaches to process: {{someVar}}
{{/defineBlock}}
{{> parent}}

dateformat

Outputting date objects as strings

  • {{#dateformat}}{{ a date object }}{{/dateformat}} outputs an isoString
  • {{#dateformat "dddd, d mmmm, yyyy"}}{{ a date object }}{{/dateformat}} outputs the date formatted as 'Tuesday, 3 February, 2014'

encode

Encoding strings to be output safely in html

  • {{encode q mode='uriComponent'}} outputs the result of encodeURIComponent(q) ({{encode q }} will also do this)
  • {{encode q mode='uri'}} outputs the result of encodeURI(q)

topicUrl

Takes a topic identifier (currently something like topic:"European%20Cars") and converts to a next stream url /stream/topic/European%20Cars

  • {{topicUrl searchString}}

paragraphs

Outputting some paragraphs from a larger chunk of html, zero indexed

  • {{{paragraphs body start=0 end=1}}} will output the first paragraph of body. Note the triple mustaches

removeImageTags

Strips all image tags from a chunk of html

  • {{{removeImageTags body}}} Note the triple mustaches

resize

Replaces an image url with an image service url, serving an appropriately resized image

  • {{#resize 200}}http://images.com/pic.jpg{{/resize}}

Logic helpers

ifEquals

Outputs contents if a thing is equal to a value

  • {{#ifEquals thing 'value'}} some content {{else}} some fallback content {{/ifEquals}}

ifAll

Outputs contents if a number of things are truthy Note that handlebars has a slightly odd understanding of truthiness

  • {{#ifAll thing1 thing2 thing3}} some content {{else}} some fallback content {{/ifAll}}

ifSome

Outputs contents if at least one of a number of things is truthy Note that handlebars has a slightly odd understanding of truthiness

  • {{#ifSome thing1 thing2 thing3}} some content {{else}} some fallback content {{/ifSome}}