You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/index.md
+106-3
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,12 @@ layout: home
9
9
10
10
**Serbea**. Finally, something to crow(n) about. _Le roi est mort, vive le roi!_
11
11
12
+
<asidemarkdown="block">
13
+
14
+
==New in Serbea 2.0!== You can now add "pipeline operator" functionality to _any_ Ruby template or class! [Check out the documentation below.](#add-pipelines-to-any-ruby-templates)
15
+
16
+
</aside>
17
+
12
18
### Table of Contents
13
19
{:.no_toc}
14
20
* …
@@ -43,10 +49,11 @@ layout: home
43
49
[10, 20, 30]
44
50
```
45
51
52
+
* Alternatively, ==now in Serbea 2.0== you can execute pipelines in plain ol' Ruby within any template or class! [See documentation below.](#add-pipelines-to-any-ruby-templates)
46
53
* Serbea will HTML autoescape variables by default within pipeline (`{{ }}`) tags. Use the `safe` / `raw` or `escape` / `h` filters to control escaping on output.
47
54
* Directives apply handy shortcuts that modify the template at the syntax level before processing through Ruby.
48
55
49
-
`{%@ %}` is a shortcut for rendering either string-named partials (`render "tmpl"`) or object instances (`render MyComponent.new`). And in Rails, you can use Turbo Stream directives for extremely consise templates:
56
+
`{%@ %}` is a shortcut for rendering either string-named partials (`render "tmpl"`) or object instances (`render MyComponent.new`). And in Rails, you can use Turbo Stream directives for extremely concise templates:
50
57
51
58
```serbea
52
59
{%@remove "timeline-read-more" %}
@@ -222,7 +229,7 @@ Serbea is an excellent upgrade from Liquid as the syntax initially looks familar
222
229
223
230
Out of the box, you can name pages and partials with a `.serb` extension. But for even more flexibility, you can add `template_engine: serbea` to your `bridgetown.config.yml` configuration. This will default all pages and documents to Serbea unless you specifically use front matter to choose a different template engine (or use an extension such as `.liquid` or `.erb`).
224
231
225
-
Here's an abreviated example of what the Post layout template looks like on the [RUBY3.dev](https://www.ruby3.dev) blog:
232
+
Here's an abreviated example of what the Post layout template looks like on the [Fullstack Ruby](https://www.fullstackruby.dev) blog:
226
233
227
234
{% raw %}
228
235
```serb
@@ -286,6 +293,102 @@ which is _far_ easier to parse visually and less likely to cause bugs due to nes
286
293
287
294
{% endraw %}
288
295
296
+
### Add Pipelines to Any Ruby Templates
297
+
298
+
New in Serbea 2.0, you can use a pipeline operator (`|`) within a `pipe` block to construct a series of expressions which continually operate on the latest state of the base value.
299
+
300
+
All you have to do is include `Serbea::Pipeline::Helper` inside of any Ruby class or template environment (aka ERB).
PipelineExample.new.output.value # => HELLO, WORLD
318
+
```
319
+
320
+
As you can see, a number of interesting things are happening here. First, we're kicking off the pipeline using a string value. This then lets us access the string's `upcase` and `split` methods. Once the string has become an array, we pipe that into our custom `test_join` method where we can call the array value's `join` method to convert it back to a string. Finally, we return the output value of the pipeline.
321
+
322
+
Like in native Serbea template pipelines, every expression in the pipeline will either call a method on the value itself, or a filter-style method that's available within the calling object. As you might expect in, say, an ERB template, all of the helpers are available as pipeline filters. In Rails, for example:
323
+
324
+
```erb
325
+
Link: <%= pipe("nav.page_link") { t | link_to(my_page_path) } %>
The length of the pipe code is slightly longer, but it's easier to follow the order of operations:
335
+
336
+
1. First, you start with the translation key.
337
+
2. Second, you translate that into official content.
338
+
3. Third, you pass that content to `link_to` along with a URL helper.
339
+
340
+
There are all sorts of uses for a pipeline, not just in templates. You could construct an entire data flow with many transformation steps. And because the pipeline operator `|` is actually optional when using a multi-line block, you can just write a series of simple Ruby statements:
341
+
342
+
```ruby
343
+
deftransform(input_value)
344
+
pipe input_value do
345
+
transform_this_way
346
+
transform_that_way
347
+
add_more_data(more_data)
348
+
convert_to_whatever # maybe this is called on the value object itself
349
+
value ->{ AnotherClass.operate_on_value _1 } # return a new value from outside processing
Pipelines "inherit" their calling context by using Ruby's `binding` feature. That's how they know how to call the methods which are available within the caller.
391
+
392
+
Another interesting facet of Serbea pipelines is that they're forgiving by default. If a filter can't be found (either there's no method available to call the object itself nor is there a separate helper method), it will log a warning to STDERR and continue on. This is to make the syntax feel a bit more like HTML and CSS where you can make a mistake or encounter an unexpected error condition yet not crash the entire application.
393
+
394
+
If you do want to crash your entire application (😜), you can set the configuration option: `Serbea::Pipeline.raise_on_missing_filters =true`. This will raise a `Serbea::FilterMissing` error if a filter can't be found.
0 commit comments