mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
renamed inc -> increment
added decrement as well (pre-decremented)
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
require 'rubygems'
|
||||||
|
require 'ruby-debug'
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
|
||||||
|
# decrement is used in a place where one needs to insert a counter
|
||||||
|
# into a template, and needs the counter to survive across
|
||||||
|
# multiple instantiations of the template.
|
||||||
|
# NOTE: decrement is a pre-decrement, --i,
|
||||||
|
# while increment is post: i++.
|
||||||
|
#
|
||||||
|
# (To achieve the survival, the application must keep the context)
|
||||||
|
#
|
||||||
|
# if the variable does not exist, it is created with value 0.
|
||||||
|
|
||||||
|
# Hello: {% decrement variable %}
|
||||||
|
#
|
||||||
|
# gives you:
|
||||||
|
#
|
||||||
|
# Hello: -1
|
||||||
|
# Hello: -2
|
||||||
|
# Hello: -3
|
||||||
|
#
|
||||||
|
class Decrement < Tag
|
||||||
|
def initialize(tag_name, markup, tokens)
|
||||||
|
@variable = markup.strip
|
||||||
|
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(context)
|
||||||
|
value = context.environments.first[@variable] ||= 0
|
||||||
|
value = value - 1
|
||||||
|
context.environments.first[@variable] = value
|
||||||
|
value.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
end
|
||||||
|
|
||||||
|
Template.register_tag('decrement', Decrement)
|
||||||
|
end
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
#require 'rubygems'
|
require 'rubygems'
|
||||||
#require 'ruby-debug'
|
require 'ruby-debug'
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
|
|
||||||
# inc is used in a place where one needs to insert a counter
|
# increment is used in a place where one needs to insert a counter
|
||||||
# into a template, and needs the counter to survive across
|
# into a template, and needs the counter to survive across
|
||||||
# multiple instantiations of the template.
|
# multiple instantiations of the template.
|
||||||
|
# (To achieve the survival, the application must keep the context)
|
||||||
#
|
#
|
||||||
# if the variable does not exist, it is created with value 0.
|
# if the variable does not exist, it is created with value 0.
|
||||||
|
|
||||||
# Hello: {% inc variable %}
|
# Hello: {% increment variable %}
|
||||||
#
|
#
|
||||||
# gives you:
|
# gives you:
|
||||||
#
|
#
|
||||||
@@ -17,7 +18,7 @@ module Liquid
|
|||||||
# Hello: 1
|
# Hello: 1
|
||||||
# Hello: 2
|
# Hello: 2
|
||||||
#
|
#
|
||||||
class Inc < Tag
|
class Increment < Tag
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
@variable = markup.strip
|
@variable = markup.strip
|
||||||
|
|
||||||
@@ -33,5 +34,5 @@ module Liquid
|
|||||||
private
|
private
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('inc', Inc)
|
Template.register_tag('increment', Increment)
|
||||||
end
|
end
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/helper'
|
|
||||||
|
|
||||||
|
|
||||||
class IncTagTest < Test::Unit::TestCase
|
|
||||||
include Liquid
|
|
||||||
|
|
||||||
def test_inc
|
|
||||||
assert_template_result('0','{%inc port %}', {})
|
|
||||||
assert_template_result('0 1','{%inc port %} {%inc port%}', {})
|
|
||||||
assert_template_result('0 0 1 2 1',
|
|
||||||
'{%inc port %} {%inc starboard%} ' +
|
|
||||||
'{%inc port %} {%inc port%} ' +
|
|
||||||
'{%inc starboard %}', {})
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
|
||||||
|
class IncTagTest < Test::Unit::TestCase
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_inc
|
||||||
|
assert_template_result('0','{%increment port %}', {})
|
||||||
|
assert_template_result('0 1','{%increment port %} {%increment port%}', {})
|
||||||
|
assert_template_result('0 0 1 2 1',
|
||||||
|
'{%increment port %} {%increment starboard%} ' +
|
||||||
|
'{%increment port %} {%increment port%} ' +
|
||||||
|
'{%increment starboard %}', {})
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_dec
|
||||||
|
assert_template_result('9','{%decrement port %}', { 'port' => 10})
|
||||||
|
assert_template_result('-1 -2','{%decrement port %} {%decrement port%}', {})
|
||||||
|
assert_template_result('1 5 2 2 5',
|
||||||
|
'{%increment port %} {%increment starboard%} ' +
|
||||||
|
'{%increment port %} {%decrement port%} ' +
|
||||||
|
'{%decrement starboard %}', { 'port' => 1, 'starboard' => 5 })
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user