DEV Community

Cover image for TIL: Template Strict Locals (TSL)
Augusts Bautra
Augusts Bautra

Posted on

TIL: Template Strict Locals (TSL)

The other day I was giving Chris' "Powerful Rails Features You Might Not Know" presentation a look, and among many useful tips (date and time helpers, nice) was a particular gem - Template Strict Locals.

Up till now I had been leaving comments like this in view partials:

<%# Partial requires `user: @user, group: user_group` %>
Enter fullscreen mode Exit fullscreen mode

But now we have something better, magic comment that will actually enforce the locals!

<%# locals: (user:, user_group:, last_signed_in: nil) -%>
Enter fullscreen mode Exit fullscreen mode

You can also have optional locals with a default value, and even specify a partial that must not be passed any locals:

# group is now optional
<%# locals: (user:, user_group: nil) -%>

# no locals, please
<%# locals: () -%> 
Enter fullscreen mode Exit fullscreen mode

Title credit to Chris Oliver.

Top comments (0)