-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
191 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |