rblade 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76b47c51f5796359252d33fe3865bb7189cd4de068579fee57520c881879755d
4
- data.tar.gz: ba1288cd0382290deb526f4faf4c5fe7c15d78372fca5248d41b4861f1ac6abb
3
+ metadata.gz: 92480d7f16a4aa87c7b2680f9db5581cac5a2dc9a4e6ffeda163e279ce3910a4
4
+ data.tar.gz: fe0b611017478bcb7f1a4c12e5deb5356b85dd85cfbc91b63cdee41dd76d8c6d
5
5
  SHA512:
6
- metadata.gz: 4783a57cbe6b7f2173f0435b2024ed3c83dff68819637d2eb759addd7ca485fd7b6d3c89987c8a07ff2561bded344037161bbb67b7bbc8147575e9adc568ce1d
7
- data.tar.gz: 91f27168157d7448e7e26979a4dc60a731a6471027a8c5ddceb5ac4e23d51f3b39de0d61a2ba1970ec58bc726ccdb3316ed5e6389c463b63c61dd358e8e64f2f
6
+ metadata.gz: 3c92d262b7b478a88cd377d0f35ff789dc80e18485a0e39f1e2a46a491b3fa5b48fa6562be623dde9a4007d0d58f9e85f6c4cbb8efcbe9b3c6b670dcc0d9cd8c
7
+ data.tar.gz: f6504a3e463cfd0a51cc8c76a78fc27db62aaace6d517a73f52fd7e1f2ec70d0bc65a2892d8f699fae27673554d624a5dcb60fbfeb2f894bf896c32a7cb95b12
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## 1.0.0 [UNRELEASED]
1
+ ## 1.0.1 [2024-08-09]
2
+ - Automatically detect when properties are used as slots
3
+ - Change `_required` to `required` in @props
4
+
5
+ ## 1.0.0 [2024-08-06]
2
6
  - Add quick reference and examples
3
7
  - Add @shouldRender directive
4
8
  - Add support for ERB style `<%==` unsafe prints
data/README.md CHANGED
@@ -5,11 +5,25 @@ RBlade is a simple, yet powerful templating engine for Ruby on Rails, inspired b
5
5
 
6
6
  RBlade template files use the `.rblade` file extension and are typically stored in the `app/views` directory.
7
7
 
8
- <a name="displaying-data"></a>
8
+
9
+ <a name="getting-started"></a>
10
+ ## Getting Started
11
+
12
+ Add RBlade to your Rails project by adding it to your Gemfile:
13
+
14
+ ```
15
+ bundle add rblade
16
+ ```
17
+
18
+ RBlade will automatically be detected and start parsing templates ending in `.rblade`.
19
+
20
+ For a quick overview of RBlade's capabilities, refer to the [reference file](REFERENCE.md) or take a look at the [examples](examples/).
21
+
22
+ <a name="table-of-contents"></a>
9
23
  ## Table of Contents
10
24
  - [RBlade Templates](#rblade-templates)
11
- * [Table of Contents](#table-of-contents)
12
25
  * [Getting Started](#getting-started)
26
+ * [Table of Contents](#table-of-contents)
13
27
  * [Displaying Data](#displaying-data)
14
28
  + [HTML Entity Encoding](#html-entity-encoding)
15
29
  + [RBlade and JavaScript Frameworks](#rblade-and-javascript-frameworks)
@@ -45,20 +59,6 @@ RBlade template files use the `.rblade` file extension and are typically stored
45
59
  + [Method Field](#method-field)
46
60
  * [Stacks](#stacks)
47
61
 
48
- <a name="getting-started"></a>
49
- ## Getting Started
50
-
51
- Add RBlade to your Rails project by adding it to your Gemfile:
52
-
53
- ```
54
- bundle add rblade
55
- ```
56
-
57
- RBlade will automatically be detected and start parsing templates ending in `.rblade`.
58
-
59
- For a quick overview of RBlade's capabilities, refer to the [reference file](REFERENCE.md) or take a look at the [examples](examples/README.md).
60
-
61
-
62
62
  <a name="displaying-data"></a>
63
63
  ## Displaying Data
64
64
 
@@ -475,11 +475,11 @@ You can define a component's data properties using a `@props` directive at the t
475
475
 
476
476
  ```rblade
477
477
  {{-- alert.rblade --}}
478
- @props({type: "warning", message: _required})
478
+ @props({type: "warning", message: required})
479
479
  <div class="{{ type }}">{{ message }}</div>
480
480
  ```
481
481
 
482
- The `@props` directive accepts a Hash where the key is the name of the attribute, and the value is the default value for the property. You can use the special `_required` value to represent a property with no default that must always be defined:
482
+ The `@props` directive accepts a Hash where the key is the name of the attribute, and the value is the default value for the property. You can use the special `required` value to represent a property with no default that must always be defined:
483
483
 
484
484
  ```rblade
485
485
  {{-- This will give an error because the alert component requires a message propery --}}
@@ -489,7 +489,7 @@ The `@props` directive accepts a Hash where the key is the name of the attribute
489
489
  All properties in the `@props` directive are automatically removed from `attributes`. Properties with names that aren't valid Ruby variable names or are Ruby reserved keywords are not created as local variables. However, you can reference them via the `attributes` local variable:
490
490
 
491
491
  ```rblade
492
- @props({"for": _required, "data-value": nil})
492
+ @props({"for": required, "data-value": nil})
493
493
  <div>{{ attributes[:for] }} {{ attributes[:'data-value'] }}</div>
494
494
  ```
495
495
 
@@ -681,7 +681,7 @@ Sometimes a component may need to render multiple different slots in different l
681
681
 
682
682
  ```rblade
683
683
  {{-- /app/views/components/alert.rblade --}}
684
- @props({title: _required})
684
+ @props({title: required})
685
685
  <span class="alert-title">{{ title }}</span>
686
686
  <div class="alert alert-danger">
687
687
  {{ slot }}
@@ -737,8 +737,8 @@ To interact with slot attributes, you can access the `attributes` property of th
737
737
 
738
738
  ```rblade
739
739
  @props({
740
- "heading": _required,
741
- "footer": _required,
740
+ "heading": required,
741
+ "footer": required,
742
742
  })
743
743
 
744
744
  <div {{ attributes.class('border') }}>
data/REFERENCE.md CHANGED
@@ -31,7 +31,7 @@ By default, RBlade will look for components in the `app/views/components` folder
31
31
  | `<x-name attribute/>` | Pass an attribute to a component with value `true` |
32
32
  | `<x-name {{ attributes }}/>` | Pass attributes to a child component |
33
33
  | `@props({header: "Header"})` | Remove `header` from the attributes Hash and introduce it as a local variable, using the specified value as a default |
34
- | `@props({header: _required})` | Remove `header` from the attributes Hash and introduce it as a local variable, raising an error if it is not set |
34
+ | `@props({header: required})` | Remove `header` from the attributes Hash and introduce it as a local variable, raising an error if it is not set |
35
35
  | `{{ slot }}` | Output the block content passed into the current component |
36
36
  | `<x-name><x-slot::header><h1>Header</h1><//>Content<//>` | Pass a named block to a component |
37
37
  | `{{ header }}` | Output the contents of a named block |
@@ -1,4 +1,4 @@
1
- @props({alert: _required})
1
+ @props({alert: required})
2
2
  @shouldRender(alert.present?)
3
3
  <p class="py-2 px-3 bg-amber-100 border border-amber-500">
4
4
  @@alert
@@ -1,4 +1,4 @@
1
- @props({title: _required})
1
+ @props({title: required})
2
2
  <!DOCTYPE html>
3
3
  <html lang="en">
4
4
  <head>
@@ -11,7 +11,7 @@ module RBlade
11
11
  "unless(#{args[0]});return'';end;"
12
12
  end
13
13
 
14
- def compileProps args
14
+ def compileProps args, tokens
15
15
  if args&.count != 1
16
16
  raise StandardError.new "Props statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
17
17
  end
@@ -19,14 +19,20 @@ module RBlade
19
19
  props = extractProps args[0]
20
20
  props.map do |key, value|
21
21
  compiled_code = ""
22
- if value == "_required"
23
- compiled_code << "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
24
- compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'];attributes.delete :'#{RBlade.escape_quotes(key)}';"
25
- elsif isValidVariableName key
26
- compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'].nil? ? #{value} : attributes[:'#{RBlade.escape_quotes(key)}'];"
27
- compiled_code << "attributes.delete :'#{RBlade.escape_quotes(key)}';"
22
+
23
+ # `_required` is deprecated. Use `required`. To be removed in 2.0.0
24
+ compiled_code << if value == "_required" || value == "required"
25
+ "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
28
26
  else
29
- compiled_code << "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
27
+ "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
28
+ end
29
+
30
+ if isValidVariableName key
31
+ compiled_code << if variableIsSlot key, tokens
32
+ "#{key}=RBlade::SlotManager.wrap(attributes.delete :'#{RBlade.escape_quotes(key)}');"
33
+ else
34
+ "#{key}=attributes.delete :'#{RBlade.escape_quotes(key)}';"
35
+ end
30
36
  end
31
37
 
32
38
  compiled_code
@@ -83,6 +89,11 @@ module RBlade
83
89
 
84
90
  true
85
91
  end
92
+
93
+ # Automatically detect if a variable is a slot by looking for "<var>.attributes"
94
+ def variableIsSlot name, tokens
95
+ tokens.any? { |token| token.value.to_s.match "#{name}.attributes" }
96
+ end
86
97
  end
87
98
  end
88
99
  end
@@ -24,6 +24,11 @@ module RBlade
24
24
  @content.respond_to?(method_name)
25
25
  end
26
26
 
27
+ # Wraps var in a slot manager, if it isn't already
28
+ def self.wrap var
29
+ var.is_a?(self) ? var : new(var)
30
+ end
31
+
27
32
  attr_reader :attributes
28
33
  end
29
34
  end
data/rblade.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rblade"
3
- s.version = "1.0.0"
3
+ s.version = "1.0.1"
4
4
  s.summary = "A component-first templating engine for Rails"
5
5
  s.description = "RBlade is a simple, yet powerful templating engine for Ruby on Rails, inspired by Laravel Blade."
6
6
  s.authors = ["Simon J"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rblade
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon J
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest