Skip to content

Releases: phlex-ruby/phlex

2.1.1

07 Mar 10:24
Compare
Choose a tag to compare

[Experimental] support for ERB snippets

In this non-breaking release, we’ve added support for ERB snippets. Anything that you would have defined as a Ruby method, e.g. view_template can now be defined as ERB.

Basic inline example

In this example, we create a Nav component and define its view_template using ERB instead of Ruby. The erb class method defines the view_template method with the compiled ERB for us. We also expose an item snippet — again defined with ERB. This time it defines the item instance method. Here, we specify locals: %(href:), which means the method will be defined as def item(href:).

class Nav < Phlex::HTML
  erb :view_template, <<~ERB
    <nav>
      <% yield %>
    </nav>
  ERB
  
  erb :item, <<~ERB, locals: %(href:)
    <a href="<%= href %>">
      <% yield %>
    </a>
  ERB
end

Locals

You can put anything in the locals string that you would regularly put in a Ruby method signature:

Required positional argument

erb :method_name, <<~ERB, locals: %(foo)

Optional positional argument

erb :method_name, <<~ERB, locals: %(foo = nil)

Positional argument with default

erb :method_name, <<~ERB, locals: %(foo = "test")

Required keyword argument

erb :method_name, <<~ERB, locals: %(foo:)

Optional keyword argument

erb :method_name, <<~ERB, locals: %(foo: nil)

Keyword argument with default

erb :method_name, <<~ERB, locals: %(foo: "test")

Note: we may rename locals: to params: or something else. Send us your feedback.

Sidecar templates

Instead of defining the ERB templates inline, you can alternatively define them as sidecar files. You’ll still need to define the method signatures in your component. Let’s go back to our nav component. We can remove the inline ERB.

class Nav < Phlex::HTML
  erb :view_template
  erb :item, locals: %(href:)
end

If this Nav component is defined in app/components/nav.rb, Phlex will search for the following sidecar files:

  1. app/components/nav/view_template.html.erb
  2. app/components/nav/item.html.erb

In the case of the view_template snippet, If Phlex can’t find app/components/nav/view_template.html.erb, it will also try app/components/nav.html.erb.

Performance

All ERB templates are compiled into methods once at boot so performance should be excellent.

Compatibility

For the most part, these ERB snippets should be completely compatible with Ruby snippets and all the Phlex features like fragments, caching, etc. When it comes to caching, the technique we use to bust the cache when you make changes in development mode with Rails will not work if you use external sidecar ERB files.

Security

Because ERB is not structural, we can’t provide all the same security features. It is possible, for example to write ERB like this, and since we don’t know you’re writing to an href attribute, we can’t strip javascript: from the start of the href.

<a href="<%= user_data %>">Click me</a>

The main concern with inline ERB support was making sure you couldn’t unintentionally interpolate unescaped user data with #{} interpolation. Our solution to this is to only support the definition of ERB snippets at the class level where it’s very unlikely any user data will be in scope.

The correct way to output user data is with <%= %> tags, though it can’t be as safe as regular Phlex output because it is string-based, not structural.

PRs

  • [EXPERIMENTAL] Add support for ERB snippets and sidecar templates by @joeldrapper in #867

Full Changelog: 2.1.0...2.1.1

2.1.0

05 Mar 11:53
Compare
Choose a tag to compare

Highlights

  • Building CSVs is now faster and we deprecated view_template for row_template in Phlex::CSV views.
  • You can now use CDATA sections in Phlex::SVG components with the new cdata method. Phlex is no longer a “leaky abstraction”.
  • There’s a new json_escape helper available on Phlex::HTML and Phlex::SVG components for escaping JSON inside JavaScript strings..

PRs

New Contributors

Full Changelog: 2.0.2...2.1.0

2.0.2

16 Feb 12:49
Compare
Choose a tag to compare

What's Changed

If you try to access context before the component starts being rendered (e.g. from your initializer), it will now raise a helpful error message. This is non-breaking because it was always raising an error message. The previous error message was that it couldn’t find the method user_context on nil.

Full Changelog: 2.0.1...2.0.2

2.0.1

15 Feb 21:35
Compare
Choose a tag to compare

What's Changed

This non-breaking release fixes a regression that meant the render? predicate was called before the context was set. This meant an error would be raised if you called out to a helper in the render? method.

Full Changelog: 2.0.0...2.0.1

2.0.0

14 Feb 17:00
Compare
Choose a tag to compare

What's Changed

Many things [updated releases notes coming soon.]

New Contributors

Full Changelog: 1.11.0...2.0.0

2.0.0.rc1

26 Nov 21:21
Compare
Choose a tag to compare
2.0.0.rc1 Pre-release
Pre-release
Merge branch 'main' of https://github.com/phlex-ruby/phlex

1.11.0

06 Jul 10:37
Compare
Choose a tag to compare

What's Changed

  • Phlex::Kit is no longer experimental! 🎉
  • Improved the way Phlex::Kit loads constants when a components’ method is called prior to the constant being loaded.
  • Improved the deprecation warning when defining the template method. The warning now tells you which class to fix.
  • We now warn on double-render and the use of non standard id attributes

PRs

Full Changelog: 1.10.3...1.11.0

1.10.3

01 Jun 14:51
Compare
Choose a tag to compare

You can now pass arguments to capture, which will be yielded to the block.

Full Changelog: 1.10.2...1.10.3

1.10.2

29 Apr 13:36
Compare
Choose a tag to compare

Includes security fixes for GHSA-9p57-h987-4vgx

1.9.3

29 Apr 13:36
Compare
Choose a tag to compare

Includes security fixes for GHSA-9p57-h987-4vgx