twig_ruby 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +218 -0
- data/lib/tasks/twig_parity.rake +1 -1
- data/lib/twig/environment.rb +1 -1
- data/lib/twig/extension/core.rb +1 -1
- data/lib/twig/loader/{array.rb → hash.rb} +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d31d9c283bb71461856d6ddc7ee8b86b9364196d0b507a6ff9e6eceefcfb4e3
|
4
|
+
data.tar.gz: d364efbe75c87f5b1f1542162e441b8f92071d7414a767f43998eb3c191927ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 127f35f5679f70bf0ea083240bb122c946fd9ce4f0fa3e0837b1d1b9127986d96619c568862bd9f08eeff3be6f02c8df23313264ab21201333dfa9626523fe15
|
7
|
+
data.tar.gz: 30f7791e2f70e9934ff2fff4d58cc7e44e18762d9bade2b3a910b5f4bff987e86e612feb501e8a3644e009e1721d7f511c47881cd75da5be4af2def7c88382d9
|
data/README.md
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
# twig-ruby
|
2
|
+
|
3
|
+
Implementation of [Twig](https://twig.symfony.com/) in Ruby.
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
|
7
|
+
- [Installation](#installation)
|
8
|
+
- [Quick Start](#quick-start)
|
9
|
+
- [Template Features](#template-features)
|
10
|
+
- [Filters](#filters)
|
11
|
+
- [Functions](#functions)
|
12
|
+
- [Tests](#tests)
|
13
|
+
- [Tags](#tags)
|
14
|
+
- [Rails Integration](#rails-integration)
|
15
|
+
- [Advanced Features](#advanced-features)
|
16
|
+
- [Configuration](#configuration)
|
17
|
+
- Advanced Docs
|
18
|
+
- [Loaders](doc/loaders.md)
|
19
|
+
- [Extensions](doc/extensions.md)
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
```bash
|
24
|
+
bundle add twig-ruby
|
25
|
+
```
|
26
|
+
|
27
|
+
## Quick Start
|
28
|
+
|
29
|
+
Here's a simple example to get you started:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'twig_ruby'
|
33
|
+
|
34
|
+
# Create a loader with your templates
|
35
|
+
loader = Twig::Loader::Hash.new({
|
36
|
+
'hello.twig' => 'Hello {{ name }}!'
|
37
|
+
})
|
38
|
+
|
39
|
+
# Create environment and render
|
40
|
+
environment = Twig::Environment.new(loader)
|
41
|
+
template = environment.load('hello.twig')
|
42
|
+
puts template.render({ name: "World" })
|
43
|
+
# Output: Hello World!
|
44
|
+
```
|
45
|
+
|
46
|
+
Or from your file system:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
loader = Twig::Loader::Filesystem.new(__dir__, ['app/views'])
|
50
|
+
```
|
51
|
+
|
52
|
+
## Template Features
|
53
|
+
|
54
|
+
Twig has the notion of Filters, Functions, and Tests
|
55
|
+
|
56
|
+
### Filters
|
57
|
+
|
58
|
+
Filters are used to modify variables.
|
59
|
+
|
60
|
+
```twig
|
61
|
+
{{ "hello"|capitalize }} {# Hello #}
|
62
|
+
{{ ["Hello", "World"]|join(" ") }} {# Hello World #}
|
63
|
+
```
|
64
|
+
|
65
|
+
### Functions
|
66
|
+
|
67
|
+
Functions are used to generate content.
|
68
|
+
|
69
|
+
```twig
|
70
|
+
{{ max([1, 2, 3]) }} {# 3 #}
|
71
|
+
{{ include("other.twig") }} {# contents of other.twig #}
|
72
|
+
```
|
73
|
+
|
74
|
+
### Tests
|
75
|
+
|
76
|
+
Tests are used to evaluate variables.
|
77
|
+
|
78
|
+
```twig
|
79
|
+
{{ 2 is even ? 'yup' : 'nope' }} {# yup #}
|
80
|
+
{{ ([1, 2, 3] has some n => n % 2 == 0) ? 'yup' : 'nope' }} {# yup #}
|
81
|
+
```
|
82
|
+
|
83
|
+
### Tags
|
84
|
+
|
85
|
+
Tags are used to control the logic of the template.
|
86
|
+
|
87
|
+
```twig
|
88
|
+
{% if n > 1 %}
|
89
|
+
Some
|
90
|
+
{% else %}
|
91
|
+
None
|
92
|
+
{% endif %}
|
93
|
+
```
|
94
|
+
|
95
|
+
```twig
|
96
|
+
<ul>
|
97
|
+
{% for i in [1, 2, 3] %}
|
98
|
+
<li>Item {{ i }}</li>
|
99
|
+
{% endfor %}
|
100
|
+
</ul>
|
101
|
+
```
|
102
|
+
|
103
|
+
## Rails Integration
|
104
|
+
|
105
|
+
This gem includes a Railtie that will automatically add your views folder and
|
106
|
+
register a `:twig` template handler. Just simply create your views such as
|
107
|
+
`app/views/welcome/index.html.twig` and it will start rendering them.
|
108
|
+
|
109
|
+
```twig
|
110
|
+
{# welcome/index.html.twig #}
|
111
|
+
|
112
|
+
{% extends 'base.html.twig' %}
|
113
|
+
|
114
|
+
{% block body %}
|
115
|
+
Welcome to my site!
|
116
|
+
{% endblock %}
|
117
|
+
```
|
118
|
+
|
119
|
+
Since Twig supports layouts through template inheritance (which is more flexible than Rails layouts),
|
120
|
+
you'll typically want to use Twig's inheritance system instead of Rails layouts. This allows you to:
|
121
|
+
|
122
|
+
1. Create a base template with common structure
|
123
|
+
2. Override specific blocks in child templates
|
124
|
+
3. Nest layouts for more complex page structures
|
125
|
+
|
126
|
+
To use Twig's inheritance instead of Rails layouts, disable Rails layouts in your controller:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class ApplicationController < ActionController::Base
|
130
|
+
layout false
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
## Configuration
|
135
|
+
|
136
|
+
These are all the defaults. You only need this configuration if you plan to change anything.
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
Rails.application.configure do
|
140
|
+
config.twig.root = ::Rails.root # Used for default Filesystem Loader
|
141
|
+
config.twig.paths = %w[/ app/views/] # Used for default Filesystem Loader
|
142
|
+
config.twig.debug = ::Rails.env.development?
|
143
|
+
config.twig.allow_helper_methods = true
|
144
|
+
config.twig.cache = ::Rails.root.join('tmp/cache/twig').to_s
|
145
|
+
config.twig.charset = 'UTF-8'
|
146
|
+
config.twig.strict_variables = true
|
147
|
+
config.twig.autoescape = :name
|
148
|
+
config.twig.auto_reload = nil
|
149
|
+
config.twig.loader = lambda do
|
150
|
+
::Twig::Loader::Filesystem.new(
|
151
|
+
current.root,
|
152
|
+
current.paths
|
153
|
+
)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
The loader is memoized as late as possible, so if you need to actually access the loader instance, you can
|
159
|
+
use `Twig.loader` to create the instance. If you do this, you can no longer set a new loader with the config
|
160
|
+
or paths. You would need to use any available methods on the loader to alter it:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
config.after_initialize do
|
164
|
+
Twig.loader.prepend_path('app/views/theme', 'theme')
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
If you plan to create your own loader that loads templates from another source like the database, you can provide
|
169
|
+
a different lamba in the config for initializing it.
|
170
|
+
|
171
|
+
## Advanced Features
|
172
|
+
|
173
|
+
Twig Ruby supports symbols as Ruby does and can be used in places strings can as
|
174
|
+
hash keys, arguments, etc.
|
175
|
+
|
176
|
+
```twig
|
177
|
+
{{ name[:first] }}
|
178
|
+
{{ user_func(:first, :second) }}
|
179
|
+
{% set hash = { key: :value } %}
|
180
|
+
```
|
181
|
+
|
182
|
+
Since Ruby has the concept of blocks, a new tag is introduced call `yield` it
|
183
|
+
can be used with helpers like `form_with`
|
184
|
+
|
185
|
+
```twig
|
186
|
+
{% yield form_with(url: 'login') do |f| %}
|
187
|
+
{{ f.email_field(:email) }}
|
188
|
+
{% endyield %}
|
189
|
+
```
|
190
|
+
### Cache Tag
|
191
|
+
|
192
|
+
The way the `cache` tag works in Rails is that it captures output from the buffer that
|
193
|
+
sends the contents of the response. Twig cannot do this prematurely since a cache might be used within
|
194
|
+
a block or other callable meant to return the string. There is a cache tag to handle this instead
|
195
|
+
that is passed the same arguments it normally would, but has extra code to capture the cache.
|
196
|
+
Using `{% yield cache() do %}` WILL NOT WORK CORRECTLY.
|
197
|
+
|
198
|
+
```twig
|
199
|
+
{% cache(product) %}
|
200
|
+
...
|
201
|
+
{% endcache %}
|
202
|
+
```
|
203
|
+
|
204
|
+
Macros can also use Ruby notation for default values:
|
205
|
+
|
206
|
+
Typical Twig:
|
207
|
+
```twig
|
208
|
+
{% macro input(name, value, type = "text", size = 20) %}
|
209
|
+
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
|
210
|
+
{% endmacro %}
|
211
|
+
```
|
212
|
+
|
213
|
+
Twig Ruby (Both versions work)
|
214
|
+
```twig
|
215
|
+
{% macro input(name, value, type: "text", size: 20) %}
|
216
|
+
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
|
217
|
+
{% endmacro %}
|
218
|
+
```
|
data/lib/tasks/twig_parity.rake
CHANGED
@@ -150,7 +150,7 @@ class TwigFixture
|
|
150
150
|
replace(template, replacements[:fixture]) + (' ' * index)
|
151
151
|
end
|
152
152
|
|
153
|
-
loader = ::Twig::Loader::
|
153
|
+
loader = ::Twig::Loader::Hash.new(gsub_templates)
|
154
154
|
environment = ::Twig::Environment.new(loader, {
|
155
155
|
cache: false,
|
156
156
|
strict_variables: true,
|
data/lib/twig/environment.rb
CHANGED
data/lib/twig/extension/core.rb
CHANGED
@@ -997,7 +997,7 @@ module Twig
|
|
997
997
|
(object.respond_to?(:empty?) && object.empty?) ||
|
998
998
|
(object.respond_to?(:length) && (object.length&.== 0)) || # rubocop:disable Style/ZeroLengthPredicate
|
999
999
|
(object.respond_to?(:size) && (object.size&.== 0)) || # rubocop:disable Style/ZeroLengthPredicate
|
1000
|
-
(object.respond_to?(:to_s) && (object.to_s
|
1000
|
+
(object.respond_to?(:to_s) && (object.to_s == ''))
|
1001
1001
|
end
|
1002
1002
|
|
1003
1003
|
# @param [Environment] environment
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twig_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig Blanchette
|
8
8
|
- Fabian Potencier
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-07-
|
11
|
+
date: 2025-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,6 +44,7 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- README.md
|
47
48
|
- lib/tasks/twig_parity.rake
|
48
49
|
- lib/twig/auto_hash.rb
|
49
50
|
- lib/twig/cache/base.rb
|
@@ -83,10 +84,10 @@ files:
|
|
83
84
|
- lib/twig/extension_set.rb
|
84
85
|
- lib/twig/file_extension_escaping_strategy.rb
|
85
86
|
- lib/twig/lexer.rb
|
86
|
-
- lib/twig/loader/array.rb
|
87
87
|
- lib/twig/loader/base.rb
|
88
88
|
- lib/twig/loader/chain.rb
|
89
89
|
- lib/twig/loader/filesystem.rb
|
90
|
+
- lib/twig/loader/hash.rb
|
90
91
|
- lib/twig/node/auto_escape.rb
|
91
92
|
- lib/twig/node/base.rb
|
92
93
|
- lib/twig/node/block.rb
|
@@ -219,9 +220,11 @@ files:
|
|
219
220
|
- lib/twig_ruby.rb
|
220
221
|
homepage: https://rubygems.org/gems/twig-ruby
|
221
222
|
licenses:
|
222
|
-
-
|
223
|
+
- BSD-3-Clause
|
223
224
|
metadata:
|
225
|
+
allowed_push_host: https://rubygems.org
|
224
226
|
rubygems_mfa_required: 'true'
|
227
|
+
source_code_uri: https://github.com/isometriks/twig-ruby
|
225
228
|
rdoc_options: []
|
226
229
|
require_paths:
|
227
230
|
- lib
|