twig_ruby 0.0.2 → 0.0.3
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 +116 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de30e1d0f458a7c5e2683f725036d8cf37e0fd09238bf14b7921d17af9f08998
|
4
|
+
data.tar.gz: bfdc53936b6dc8449c44269e5bb0b717c01e02597911266404550a805cbb34b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6554b109c0e00241e6a5dac0dfd4f8c36e78b50dcc45edff041ff9907cf02609d2abd4b5cdc326ca1ee92b12217ed83a8ba420c60afc65f790bb34567fbc9af9
|
7
|
+
data.tar.gz: 70bf08c22925adf25f6b469bc7afcad62bdd3b4ed45761dbca3cd4619b1a213dcf2a3d70c1595ae7958354b2527fde438b78763c72238281971dc35c2aeaba71
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# twig-ruby
|
2
|
+
|
3
|
+
Implementation of [Twig](https://twig.symfony.com/) in Ruby.
|
4
|
+
|
5
|
+
```bash
|
6
|
+
bundle add twig-ruby
|
7
|
+
```
|
8
|
+
|
9
|
+
## Rails
|
10
|
+
|
11
|
+
This gem includes a Railtie that will automatically add your views folder and
|
12
|
+
register a `:twig` template handler. Just simply create your views such as
|
13
|
+
`app/views/welcome/index.html.twig` and it will start rendering them.
|
14
|
+
|
15
|
+
```twig
|
16
|
+
{# welcome/index.html.twig #}
|
17
|
+
|
18
|
+
{% extends 'base.html.twig' %}
|
19
|
+
|
20
|
+
{% block body %}
|
21
|
+
Welcome to my site!
|
22
|
+
{% endblock %}
|
23
|
+
```
|
24
|
+
|
25
|
+
You should add `layout false` in your `ApplicationController`
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class ApplicationController < ActionController::Base
|
29
|
+
layout false
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Rails Configuration
|
34
|
+
|
35
|
+
These are all the defaults. You only need this configuration if you plan to change anything.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
Rails.application.configure do
|
39
|
+
config.twig.root = ::Rails.root, # Used for default Filesystem Loader
|
40
|
+
config.twig.paths = %w[/ app/views/], # Used for default Filesystem Loader
|
41
|
+
config.twig.debug = ::Rails.env.development?,
|
42
|
+
config.twig.allow_helper_methods = true,
|
43
|
+
config.twig.cache = ::Rails.root.join('tmp/cache/twig').to_s,
|
44
|
+
config.twig.charset = 'UTF-8',
|
45
|
+
config.twig.strict_variables = true,
|
46
|
+
config.twig.auto_reload = nil,
|
47
|
+
config.twig.loader = lambda do
|
48
|
+
::Twig::Loader::Filesystem.new(
|
49
|
+
current.root,
|
50
|
+
current.paths
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
The loader is memoized as late as possible, so if you need to actually access the loader instance, you can
|
57
|
+
use `Twig.loader` to create the instance. If you do this, you can no longer set a new loader with the config
|
58
|
+
or paths. You would need to use any available methods on the loader to alter it:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
config.after_initialize do
|
62
|
+
Twig.loader.prepend_path('app/views/theme', 'theme')
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
If you plan to create your own loader that loads templates from another source like the database, you can provide
|
67
|
+
a different lamba in the config for initializing it.
|
68
|
+
|
69
|
+
## Additions
|
70
|
+
|
71
|
+
Twig Ruby supports symbols as Ruby does and can be used in places strings can as
|
72
|
+
hash keys, arguments, etc.
|
73
|
+
|
74
|
+
```twig
|
75
|
+
{{ name[:first] }}
|
76
|
+
{{ user_func(:first, :second) }}
|
77
|
+
{% set hash = { key: :value } %}
|
78
|
+
```
|
79
|
+
|
80
|
+
Since Ruby has the concept of blocks, a new tag is introduced call `yield` it
|
81
|
+
can be used with helpers like `form_with`
|
82
|
+
|
83
|
+
```twig
|
84
|
+
{% yield form_with(url: 'login') do |f| %}
|
85
|
+
{{ f.email_field(:email) }}
|
86
|
+
{% endyield %}
|
87
|
+
```
|
88
|
+
### Cache Tag
|
89
|
+
|
90
|
+
The way the `cache` tag works in Rails is that it captures output from the buffer that
|
91
|
+
sends the contents of the response. Twig cannot do this prematurely since a cache might be used within
|
92
|
+
a block or other callable meant to return the string. There is a cache tag to handle this instead
|
93
|
+
that is passed the same arguments it normally would, but has extra code to capture the cache.
|
94
|
+
Using `{% yield cache() do %}` WILL NOT WORK CORRECTLY.
|
95
|
+
|
96
|
+
```twig
|
97
|
+
{% cache(product) %}
|
98
|
+
...
|
99
|
+
{% endyield %}
|
100
|
+
```
|
101
|
+
|
102
|
+
Macros can also use Ruby notation for default values:
|
103
|
+
|
104
|
+
Typical Twig:
|
105
|
+
```twig
|
106
|
+
{% macro input(name, value, type = "text", size = 20) %}
|
107
|
+
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
|
108
|
+
{% endmacro %}
|
109
|
+
```
|
110
|
+
|
111
|
+
Twig Ruby (Both versions work)
|
112
|
+
```twig
|
113
|
+
{% macro input(name, value, type: "text", size: 20) %}
|
114
|
+
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
|
115
|
+
{% endmacro %}
|
116
|
+
```
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig Blanchette
|
@@ -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
|
@@ -221,7 +222,9 @@ homepage: https://rubygems.org/gems/twig-ruby
|
|
221
222
|
licenses:
|
222
223
|
- MIT
|
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
|