Skip to content

Commit

Permalink
prototype-1
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Feb 5, 2025
1 parent bfe29e1 commit d707580
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 19 deletions.
9 changes: 9 additions & 0 deletions example/server/example_servlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def products
{ '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
Expand Down
Empty file.
121 changes: 121 additions & 0 deletions example/server/templates/settings.liquid
Original file line number Diff line number Diff line change
@@ -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>
40 changes: 21 additions & 19 deletions lib/liquid/tags.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -42,6 +43,7 @@ module Tags
'if' => If,
'echo' => Echo,
'tablerow' => TableRow,
'let' => Let,
}.freeze
end
end
40 changes: 40 additions & 0 deletions lib/liquid/tags/let.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d707580

Please sign in to comment.