Skip to content

Commit

Permalink
Support kwargs-only method calls for partial (bridgetownrb#582)
Browse files Browse the repository at this point in the history
* Support kwargs-only method calls for `partial`

* Update documentation for partials
  • Loading branch information
jaredcwhite authored Jun 27, 2022
1 parent f7dd9cc commit b0a8fc4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ def h(input)
Erubi.h(input)
end

def partial(partial_name, options = {})
def partial(partial_name = nil, **options, &block)
partial_name = options[:template] if partial_name.nil? && options[:template]
options.merge!(options[:locals]) if options[:locals]
options[:content] = yield if block_given?
options[:content] = capture(&block) if block

_render_partial partial_name, options
end

def _render_partial(partial_name, options)
partial_path = _partial_path(partial_name, "erb")
tmpl = site.tmp_cache["partial-tmpl:#{partial_path}"] ||= Tilt::ErubiTemplate.new(
partial_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ module Bridgetown
class SerbeaView < ERBView
include Serbea::Helpers

def partial(partial_name, options = {}, &block)
options.merge!(options[:locals]) if options[:locals]
options[:content] = capture(&block) if block

def _render_partial(partial_name, options)
partial_path = _partial_path(partial_name, "serb")
tmpl = site.tmp_cache["partial-tmpl:#{partial_path}"] ||=
Tilt::SerbeaTemplate.new(partial_path)
Expand Down
6 changes: 3 additions & 3 deletions bridgetown-core/lib/bridgetown-core/ruby_template_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def initialize(convertible)
@site = page.site
end

def partial(_partial_name, _options = {})
def partial(_partial_name = nil, **_options)
raise "Must be implemented in a subclass"
end

def render(item, options = {}, &block)
def render(item, **options, &block)
if item.respond_to?(:render_in)
result = item.render_in(self, &block)
result&.html_safe
else
partial(item, options, &block)&.html_safe
partial(item, **options, &block)&.html_safe
end
end

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/test/source/src/_layouts/serblayout.serb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ layout_var:

{%= yield %}

{%= partial "testing/partials", locals: { yes: "yes." } %}
{%= partial template: "testing/partials", locals: { yes: "yes." } %}
{%= render "testing/partials", yes: "YES!!" %}

<footer>{%= site.time %} / {%= Bridgetown::VERSION %}</footer>
2 changes: 1 addition & 1 deletion bridgetown-website/plugins/builders/inspectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def build
# LintHTML replacement to find div/span tags
inspect_html do |document, resource|
class_allow_list = (
Array(site.config.ex_span_sion&.allowed_classes) + %w[highlighter-rouge highlight]
Array(site.config.divicide&.allowed_classes) + %w[highlighter-rouge highlight]
).uniq

tags = document.query_selector_all("div, span")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ You can also pass variables to partials using either a `locals` hash or as keywo
<%%= render "some/partial", locals: { key: "value", another_key: 123 } %>
```

As an alternative to passing the partial filename as the first argument, you can supply a `template` keyword argument instead. This makes it easier to pass all arguments via a separate hash:

```eruby
<%% options = { template: "mypartial", title: "Hello!" } %>
<%%= partial **options %>
```

Partials also support capture blocks, which can then be referenced via the `content` local variable within the partial.

## Rendering Ruby Components

For better encapsulation and reuse of Ruby-based templates as part of a "design system" for your site, we encourage you to write Ruby components using either `Bridgetown::Component` or GitHub's ViewComponent library. [Check out the documentation and code examples here](/docs/components/ruby).
Expand Down

0 comments on commit b0a8fc4

Please sign in to comment.