Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,24 @@ Usage:

Kiss my shiny metal ass!

#### ternary

Conditionally render one of 2 values given a boolean value.

Parameters:

boolean ['boolean']
value1: ['object']
value2: ['object']

Usage:

isMale = true;

{{ternary isMale "hey dude!" "hey dudette!"}}

hey dude!

## Dates

#### formatDate
Expand Down Expand Up @@ -1139,7 +1157,27 @@ Usage:
# Your template
{{partial "planet_express" data template}}

#### setVar

Sets a value on the provided context for use later.

Parameters:

name [string] - The name of the value to assign to the context.
value [string|int] - The value to assign to the variable.
context [object] - The context to assign the value to.

Usage:

{{setVar "newVariable" "value" this}}{{newValue}}

fromRemote = false
id = 5

{{ setVar "itemId" (ternary fromRemote (concat "remote-" id) (concat "local-" id)) }}{{itemId}}

value
local-5

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/elving/swag/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

4 changes: 4 additions & 0 deletions src/swag.comparisons.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ Swag.addHelper 'or', (testA, testB, options) ->
Swag.addHelper 'and', (testA, testB, options) ->
if testA and testB then options.fn this else options.inverse this
, ['safe:string|number', 'safe:string|number']

Swag.addHelper 'ternary', (boolean, value1, value2) ->
if boolean then value1 else value2
, ['boolean', 'object', 'object']
5 changes: 5 additions & 0 deletions src/swag.miscellaneous.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Swag.addHelper 'setVar', (varName, value, context) ->
context.data[varName] = value
, ['string', 'string', 'object']


Swag.addHelper 'default', (value, defaultValue) ->
value or defaultValue
, 'safe:string|number', 'string|number'
Expand Down
9 changes: 9 additions & 0 deletions test/comparisons_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ describe 'and', ->
context = great: yes, magnificent: yes

template(context).should.equal 'Kiss my glorious metal ass!'

describe 'ternary', ->
describe '{{ternary boolean value1 value2}}', ->
it 'renders the correct value given the boolean', ->
source = '{{ternary !bool "TRUE" "FALSE"}}'
template = Handlebars.compile(source)
context = bool: true

template(context).should.equal 'TRUE'
9 changes: 9 additions & 0 deletions test/miscellaneous_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe 'default', ->

template(context).should.equal 'No title available.'

describe 'setVar', ->
describe '{{ setVar "varName" "varValue" "this" }}', ->
it 'should return the value of the variable.', ->
source = '{{setVar "bookingText" "book now." this }}{{bookingText}}'
template = Handlebars.compile(source)
context =
data: {}
template(context).should.equal 'book now.'

Swag.Config.partialsPath = '../test/templates/'

describe 'partial', ->
Expand Down