Skip to content

Commit

Permalink
Top Hat changes
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Dec 5, 2024
1 parent 41a77c6 commit 4df1145
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
gemspec

gem "base64"
gem "webrick"

group :benchmark, :test do
gem 'benchmark-ips'
Expand Down
39 changes: 35 additions & 4 deletions example/server/example_servlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def paragraph(p)

class Servlet < LiquidServlet
def index
{ 'date' => Time.now }
{ 'date' => Time.now, 'products' => products_list }
end

def products
Expand All @@ -29,11 +29,42 @@ def products

private

class Name < Liquid::Drop
attr_reader :raw, :origin

def initialize(raw, origin)
super()
@raw = raw
@origin = origin
end
end

class Price < Liquid::Drop
attr_reader :value, :unit

def initialize(value, unit = 'USD')
super()
@value = value
@unit = unit
end
end

class Product < Liquid::Drop
attr_reader :name, :price, :description

def initialize(name, origin, price, description)
super()
@name = Name.new(name, origin)
@price = Price.new(price)
@description = description
end
end

def products_list
[
{ 'name' => 'Arbor Draft', 'price' => 39900, 'description' => 'the *arbor draft* is a excellent product' },
{ 'name' => 'Arbor Element', 'price' => 40000, 'description' => 'the *arbor element* rocks for freestyling' },
{ 'name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity' }
{ 'name' => 'Alpine jacket', 'price' => { 'value' => 30000, 'unit' => 'USD' }, 'description' => 'the *alpine jacket* is a excellent product' },
{ 'name' => 'Mountain boots', 'price' => { 'value' => 40000, 'unit' => 'BRL' }, 'description' => 'the *mountain boots* are perfect for hiking' },
{ 'name' => 'Safety helmet', 'price' => { 'value' => 10000, 'unit' => 'USD' }, 'description' => 'the *safety helmet* provides essential protection for winter sports' }
]
end

Expand Down
71 changes: 68 additions & 3 deletions example/server/templates/index.liquid
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
<p>Hello world!</p>
{% # theme-check-disable UnknownFilter, UndefinedObject %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Liquid filters playground</title>
<script>
setInterval(() => {
fetch(window.location.href)
.then(response => response.text())
.then(html => {
const parser = new DOMParser();
const newDoc = parser.parseFromString(html, 'text/html');
const newBody = newDoc.body;
document.body.innerHTML = newBody.innerHTML;
})
.catch(error => console.error('Error updating content:', error));
}, 1000);
</script>
<style>
body {
zoom: 2;
font-family: monospace
}
</style>
</head>
<body>

<p>It is {{date}}</p>
<h1>
Liquid filters playground
</h1>

{{ products | map: "name" | json }}

<p>Check out the <a href="/products">Products</a> screen </p>
<h2>
find
</h2>
<pre>
{% assign product = products | find: "name", "Mountain boots" %}
{{ product.name }} - {{ product.price.value }}

{% assign product = products | find: "price.unit", "USD" %}
{{ product.name }} - {{ product.price.value }}
</pre>

<h2>
find_index
</h2>
<pre>
{% assign index = products | find_index: "name", "Mountain boots" %}
{{ index }}
</pre>

<h2>
has
</h2>
<pre>
{{ products | has: "name", "Mountain boots" }}
</pre>


<h2>
reject
</h2>
<pre>
{{ products | map: "name" | join: ", " }}
{{ products | reject: "name", "Mountain boots" | map: "name" | join: ", " }}
</pre>

</body>
</html>

0 comments on commit 4df1145

Please sign in to comment.