diff --git a/README.md b/README.md index 46c0b48..4283cfc 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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") diff --git a/src/swag.comparisons.coffee b/src/swag.comparisons.coffee index 8f619de..b8ecb26 100644 --- a/src/swag.comparisons.coffee +++ b/src/swag.comparisons.coffee @@ -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'] diff --git a/src/swag.miscellaneous.coffee b/src/swag.miscellaneous.coffee index 410a45a..e787349 100644 --- a/src/swag.miscellaneous.coffee +++ b/src/swag.miscellaneous.coffee @@ -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' diff --git a/test/comparisons_test.coffee b/test/comparisons_test.coffee index d6c7402..4960ead 100644 --- a/test/comparisons_test.coffee +++ b/test/comparisons_test.coffee @@ -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' diff --git a/test/miscellaneous_test.coffee b/test/miscellaneous_test.coffee index 016c698..d2577f3 100644 --- a/test/miscellaneous_test.coffee +++ b/test/miscellaneous_test.coffee @@ -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', ->