File: render

package info (click to toggle)
ruby-liquid 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,444 kB
  • sloc: ruby: 14,571; makefile: 6
file content (46 lines) | stat: -rwxr-xr-x 909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'liquid'

class VirtualFileSystem
  def initialize
    snippet_1 = <<~LIQUID
      <h1>
        {{- greating | default: 'Hello' }}, {{ name | default: 'world' -}}!
      </h1>
    LIQUID
    snippet_2 = <<~LIQUID
      {%- for i in (1..5) -%}
        > {{ i }}
      {%- endfor -%}
    LIQUID

    @templates = {
      'snippet-1' => snippet_1,
      'snippet-2' => snippet_2,
    }
  end

  def read_template_file(key)
    @templates[key] || raise(Liquid::FileSystemError, "No such template '#{key}'")
  end
end

def source
  File.read(ARGV[0])
rescue StandardError
  'Usage: bin/render example/server/templates/index.liquid'
end

def assigns
  {
    'date' => Time.now,
  }
end

puts Liquid::Template
  .parse(source, error_mode: :strict2)
  .tap { |t| t.registers[:file_system] = VirtualFileSystem.new }
  .render(assigns)