Coder Social home page Coder Social logo

n-handlebars's Introduction

n-handlebars CircleCI

⚠️ This component is deprecated. Please use Page Kit or vanilla Handlebars to build new applications. ⚠️

Enhanced handlebars for use in next applications

Express applications

require('n-handlebars')(app, options)

Where options may have the following properties

  • directory: String - absolute path to the current application's working directory REQUIRED
  • partialsDir: Array of directories containing partials. This is concatenated to the default value of ./bower_components/
  • layoutsDir: String - the directory in which express-handlebars layouts are contained
  • defaultLayout: Name of the default layout to use. Defaults to false
  • helpers: Map of custom helpers to add to handlebars (see below for a list those included by default)
  • viewsDirectory: String - subdirectory where the application's views are stored (default /views)
  • limitToComponents: Array - limit the bower components partials included to just the specified components (e.g. ['n-ui'])
  • extname: String - file extension of handlebars templates (default: '.html')

Returns a promise which resolves when all partials in the supplied directories have been registered

To use the express-handlebars instance by itself (occassionally useful for consuming templates from bower_components outside of the context of an express app) use require('n-handlebars').standalone(options)

A handlebars instance with all next helpers, but without partials, is also exposed at require('n-handlebars').handlebars;

Client side javascript

require('n-handlebars')(options)

Where options may have the following properties

  • helpers: Map of custom helpers to add to handlebars (see below for a list those included by default)

Returns a handlebars instance

Other server side applications

Should you need to use handlebars with all the next helpers but without next-express use require('n-handlebars').handlebars(options), which has the same API a the client side module

THE HELPERS

Inheritance helpers

Block 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
<header>thing</header>
{{#outputBlock 'my-block'}}default content{{/outputBlock}}
<footer>thing</footer>

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

usePartial

Allows a partial to be selected based on the value of a variable

  • {{{usePartial variable path='path/to/partial'}}}

If variable value is foobar then partial will be path/to/partial/foobar. Note a '>' is not required in the path and you will normally need triple mustaches

Content helpers

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)

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}}

json

Outputs an object as json.

  • {{json obj}} - for use within data attributes and elsewhere in html (will convert '"' to '"' etc..)
  • {{{json obj}}} - for outputting the json unencoded

concat

Concatenates strings.

  • {{concat str1 str2}}

decodeHtmlEntities

Decodes a (very limited) safe list* of HTML entities into their respective characters (* = not &, <, >, ", ' or ``)

  • {{decodeHtmlEntities 'lorem&nbsp;ipsum&nbsp;dolar'}} outputs lorem ipsum dolar

Logic helpers

ifEquals

Outputs contents if a thing is equal to a value

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

ifEqualsSome

Outputs contents if a thing is equal to at least a value from all the values enumerated

  • {{#ifEqualsSome thing 'value1' 'value2'}} some content {{else}} some fallback content {{/ifEqualsSome}}

unlessEquals

Outputs contents if a thing is not exactly equal to a value

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

ifAll

Outputs contents if a number of things are truthy

  • {{#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

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

ifBool

Outputs contents if a complex boolean logic expression is satisfied. Uses string formatting to generate the expression

  • {{#ifBool thing1 thing2 "($0 && $1)"}} some content {{else}} some fallback content {{/ifBool}}

ifTypeof

Outputs contents if typeof operator on first argument equals the second argument

  • {{#ifTypeof object "string"}} objectis a string {{else}}object is not a string {{/ifTypeof}}

Iteration helpers

slice

Loop through a subset of items

  • {{#slice items limit="2" offset="4"}} some content {{/slice}}

increment

Displays @index value added to given argument (e.g. an argument of 1 will display ordinal not cardinal numbers, i.e. values start from 1 not 0).

  • {{#each array}}{{#increment 1}}{{@index}}{{/increment}}{{/each}}

Presenter helpers

presenter

Makes a presenter class available to the template for the current context

  • example
		{{#presenter presenterPath context hashArguments}} //1
			<h3>{{@myPresenter.title}}</h3> //2
			<p>{{subheader}}</p> //3
			<ol>
				{{#with @myPresenter.items}} //4
					<li><p>{{relatedTitle}}</p></li>
				{{/with}}
			</ol>
		{{/presenter}}
  1. Instantiating the presenter presenterPath can either be a file from the root of the application, eg. ./presenters/my-presenter or if in a bower component component/presenters/my-presenter. context is the current data context - ie. @this hashArguments is any data that you want to pass to the presenter from the template, eg. size='large' widths='1,2,3' (these can only be passed as strings) Any hashArguments are added to the data context and passed to the presenter.

  2. Retrieving data from the presenter (swap myPresenter for the camel cased file name for your presenter) @myPresenter.title will call the title function of your presenter and receive back the output of that function. This could be a text string for example.

  3. Using current data context the current data context is still available

  4. Using an array from the presenter existing helpers, eg. #each, #with #slice can be used with arrays passed back from the presenter.

n-handlebars's People

Contributors

aintgoin2goa avatar alexnaish avatar andygnewman avatar andygout avatar benbarnett avatar bjfletcher avatar charypar avatar commuterjoy avatar debugwand avatar edds avatar glynnphillips avatar i-like-robots avatar ironsidevsquincy avatar jakechampion avatar jenniferemshepherd avatar kateloschinina avatar ker-an avatar leafrogers avatar leggsimon avatar matthew-andrews avatar remybach avatar renovate[bot] avatar richard-still-ft avatar roland-vachter avatar theoleanse avatar wheresrhys avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

n-handlebars's Issues

Dependency Dashboard

This issue contains a list of Renovate updates and their statuses.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • Update dependency lintspaces-cli to ^0.7.0
  • Update dependency chai to v4
  • Update dependency mocha to v8
  • Update dependency sinon to v9
  • Update dependency supertest to v4

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Closed/Ignored

These updates were closed unmerged and will not be recreated unless you click a checkbox below.


  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>financial-times/renovate-config-next)

Update Dependencies (Renovate Bot)

This master issue contains a list of Renovate updates and their statuses.

Pending Approval

These PRs will be created by Renovate only once you click their checkbox below.

  • Update dependency lintspaces-cli to ^0.7.0
  • Update dependency chai to v4
  • Update dependency eslint to v5
  • Update dependency mocha to v6
  • Update dependency sinon to v7
  • Update dependency supertest to v4

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.