mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-13 07:50:43 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
432c171be9 |
@@ -27,6 +27,15 @@ class Servlet < LiquidServlet
|
||||
{ 'products' => products_list, 'more_products' => more_products_list, 'description' => description, 'section' => 'Snowboards', 'cool_products' => true }
|
||||
end
|
||||
|
||||
def settings
|
||||
{
|
||||
'settings' => {
|
||||
'text' => 'Liquid',
|
||||
'font' => 'Arial'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def products_list
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
<style>
|
||||
* {
|
||||
zoom: 1.1;
|
||||
font-family: Menlo;
|
||||
color: rgb(13, 13, 13);
|
||||
}
|
||||
|
||||
.preview {
|
||||
display: flex;
|
||||
gap: 2rem;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
.preview > div {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
code, pre {
|
||||
background: #f5f5f5;
|
||||
padding: 0.5rem;
|
||||
border-radius: 4px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.label {
|
||||
font-weight: bold;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<h1>let</h1>
|
||||
|
||||
<hr>
|
||||
|
||||
<div class="preview">
|
||||
<div>
|
||||
<div class="label">Liquid source</div>
|
||||
<pre>
|
||||
{%- raw -%}
|
||||
{% assign text = "My text" %}
|
||||
{{ settings }}
|
||||
{{ text }}
|
||||
{%- endraw -%}
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<div class="label">HTML output</div>
|
||||
<pre>
|
||||
{% assign text = "My text" %}
|
||||
{{ settings }}
|
||||
{{ text -}}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="preview">
|
||||
<div>
|
||||
<div class="label">Liquid source</div>
|
||||
<pre>
|
||||
{%- raw -%}
|
||||
{% let (text, font): settings %}
|
||||
text: {{ text }},
|
||||
font: {{ font }}
|
||||
{% endlet %}
|
||||
{%- endraw -%}
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<div class="label">HTML output</div>
|
||||
<pre>
|
||||
{%- let (text, font): settings %}
|
||||
text: {{ text }},
|
||||
font: {{ font }}
|
||||
{% endlet -%}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="preview">
|
||||
<div>
|
||||
<div class="label">Liquid source</div>
|
||||
<pre>
|
||||
{%- raw -%}
|
||||
{% let font: settings.font %}
|
||||
text: {{ text }},
|
||||
font: {{ font }}
|
||||
{% endlet %}
|
||||
{%- endraw -%}
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<div class="label">HTML output</div>
|
||||
<pre>
|
||||
{%- let font: settings.font %}
|
||||
text: {{ text }},
|
||||
font: {{ font }}
|
||||
{% endlet -%}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="preview">
|
||||
<div>
|
||||
<div class="label">Liquid source</div>
|
||||
<pre>
|
||||
{%- raw -%}
|
||||
Global is not impacted:
|
||||
- text: {{ text }},
|
||||
- font: {{ font }}
|
||||
{%- endraw -%}
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<div class="label">HTML output</div>
|
||||
<pre>
|
||||
Global is not impacted:
|
||||
- text: {{ text }},
|
||||
- font: {{ font }}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
+21
-19
@@ -1,24 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "tags/table_row"
|
||||
require_relative "tags/echo"
|
||||
require_relative "tags/if"
|
||||
require_relative "tags/break"
|
||||
require_relative "tags/inline_comment"
|
||||
require_relative "tags/for"
|
||||
require_relative "tags/assign"
|
||||
require_relative "tags/ifchanged"
|
||||
require_relative "tags/case"
|
||||
require_relative "tags/include"
|
||||
require_relative "tags/continue"
|
||||
require_relative "tags/capture"
|
||||
require_relative "tags/decrement"
|
||||
require_relative "tags/unless"
|
||||
require_relative "tags/increment"
|
||||
require_relative "tags/comment"
|
||||
require_relative "tags/raw"
|
||||
require_relative "tags/render"
|
||||
require_relative "tags/cycle"
|
||||
require_relative 'tags/table_row'
|
||||
require_relative 'tags/echo'
|
||||
require_relative 'tags/if'
|
||||
require_relative 'tags/break'
|
||||
require_relative 'tags/inline_comment'
|
||||
require_relative 'tags/for'
|
||||
require_relative 'tags/assign'
|
||||
require_relative 'tags/ifchanged'
|
||||
require_relative 'tags/case'
|
||||
require_relative 'tags/include'
|
||||
require_relative 'tags/continue'
|
||||
require_relative 'tags/capture'
|
||||
require_relative 'tags/decrement'
|
||||
require_relative 'tags/unless'
|
||||
require_relative 'tags/increment'
|
||||
require_relative 'tags/comment'
|
||||
require_relative 'tags/raw'
|
||||
require_relative 'tags/render'
|
||||
require_relative 'tags/cycle'
|
||||
require_relative 'tags/let'
|
||||
|
||||
module Liquid
|
||||
module Tags
|
||||
@@ -42,6 +43,7 @@ module Liquid
|
||||
'if' => If,
|
||||
'echo' => Echo,
|
||||
'tablerow' => TableRow,
|
||||
'let' => Let,
|
||||
}.freeze
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class Let < Liquid::Block
|
||||
MULTI_ASSIGNMENT = /\A\(\s*(?<vars>[^)]+)\s*\):\s*(?<source>[^\s]+)\z/
|
||||
VAR_ASSIGNMENTS = /(?<var>[\w-]+)\s*:\s*(?<expr>[^\s,]+)/
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super(tag_name, markup, options)
|
||||
@assignments = parse_markup(markup.strip)
|
||||
end
|
||||
|
||||
def render(context)
|
||||
subcontext = context.new_isolated_subcontext
|
||||
|
||||
@assignments.each do |var, expression|
|
||||
subcontext[var] = lookup(context, expression)
|
||||
end
|
||||
|
||||
super(subcontext)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_markup(markup)
|
||||
if (m = MULTI_ASSIGNMENT.match(markup))
|
||||
variables = m[:vars].split(/\s*,\s*/)
|
||||
source = m[:source]
|
||||
variables.map { |v| [v, "#{source}.#{v}"] }.to_h
|
||||
else
|
||||
markup.scan(VAR_ASSIGNMENTS).to_h
|
||||
end
|
||||
end
|
||||
|
||||
def lookup(context, expression)
|
||||
variable_lookup = Liquid::VariableLookup.new(expression)
|
||||
variable_lookup.evaluate(context)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user