diff --git a/README.md b/README.md index 4a12aa9..c9400ec 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,14 @@ Use fancy operators in assignment. {% assign title_text += ' →' if linkpost %} ``` +Use variables in assignment. + +``` +{% assign my_data = site.data.{{ data_file_name }} %} +{% assign name = site.name | append:page.{{ product_name }} | downcase %} +{% assign stupid_example = (page.{{ product_name }} == null ? 'no-product' : 'product-{{ page.product_code }} | downcase }}') %} +``` + ## Contributing 1. Fork it ( https://github.com/octopress/assign-tag/fork ) diff --git a/lib/octopress-assign-tag.rb b/lib/octopress-assign-tag.rb index 4327280..9431f35 100644 --- a/lib/octopress-assign-tag.rb +++ b/lib/octopress-assign-tag.rb @@ -7,6 +7,8 @@ module Tags module Assign class Tag < Liquid::Tag SYNTAX = /([[:word:]]+)\s*(=|\+=|\|\|=)\s*(.*)\s*/o + VAR_MATCH = /([^{]*)({{(.*)}})(.*)/o + SUB_VARS = /([^{]*)({{\s([^}]*)\s}})/o def initialize(tag_name, markup, tokens) @markup = markup @@ -22,7 +24,22 @@ def render(context) operator = $2 value = $3 + if value =~ VAR_MATCH + first_part = $1 + evaluate_part = $2 + last_part = $4 + new_value = "" + + evaluate_part.scan (SUB_VARS) { + pre_value = $1 + evaluated_var = TagHelpers::Var.get_value($3, context) + new_value = new_value + pre_value + evaluated_var + } + value = first_part + new_value + last_part + end + value = TagHelpers::Var.get_value(value, context) + return if value.nil? context = TagHelpers::Var.set_var(var, operator, value, context) diff --git a/lib/octopress-assign-tag/version.rb b/lib/octopress-assign-tag/version.rb index 693760a..2f60b6a 100644 --- a/lib/octopress-assign-tag/version.rb +++ b/lib/octopress-assign-tag/version.rb @@ -1,7 +1,7 @@ module Octopress module Tags module Assign - VERSION = "1.0.3" + VERSION = "1.0.4" end end end diff --git a/test/_expected/index.html b/test/_expected/index.html index fe649cd..0a79c3a 100644 --- a/test/_expected/index.html +++ b/test/_expected/index.html @@ -21,5 +21,15 @@ AWESOME → AWESOME whatever-man → whatever-man +## Assignment with sub variables +sub_var1 → sub_var1 +sub_var2 → sub_var2 +hello → hello +HELLO → HELLO +hello-goodbye → hello-goodbye +HELLO-GOODBYE → HELLO-GOODBYE +COMPLEX-HELLO-GOODBYE → COMPLEX-HELLO-GOODBYE +complex-hello-GOODBYE → complex-hello-GOODBYE + ## Filters on non-string variables ABab diff --git a/test/index.html b/test/index.html index 5689ae1..ae80010 100644 --- a/test/index.html +++ b/test/index.html @@ -1,6 +1,8 @@ --- layout: null test_array: [a, b, A, B] +sub_var1: hello +sub_var2: goodbye --- ## Simple assign yep → {% assign var1 = 'yep' %}{{ var1 }} @@ -25,5 +27,15 @@ AWESOME → {% assign var10 = var9 | upcase %}{{ var10 }} whatever-man → {% assign var11 = 'whatever man' || nil | replace:' ','-' %}{{ var11 }} +## Assignment with sub variables +sub_var1 → {% assign test_var1 = 'sub_var1' %}{{ test_var1 }} +sub_var2 → {% assign test_var2 = 'sub_var2' %}{{ test_var2 }} +hello → {% assign var12 = page.{{ test_var1 }} %}{{ var12 }} +HELLO → {% assign var13 = page.{{ test_var1 }} | upcase %}{{ var13 }} +hello-goodbye → {% assign var15 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} %}{{ var15 }} +HELLO-GOODBYE → {% assign var16 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} | upcase %}{{ var16 }} +COMPLEX-HELLO-GOODBYE → {% assign var17 = (page.{{ test_var1 }} == 'hello' ? 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 }}' : 'lame' ) | upcase %}{{ var17 }} +complex-hello-GOODBYE → {% assign var17 = (page.{{ test_var1 }} == null ? 'lame' : 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 | upcase }}') %}{{ var17 }} + ## Filters on non-string variables {% assign items = page.test_array | sort %}{{ items }}