Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify access to data syntax and Roda rendering #890

Merged
merged 4 commits into from
May 4, 2024

Conversation

jaredcwhite
Copy link
Member

@jaredcwhite jaredcwhite commented Apr 28, 2024

After writing data.xyz a zillion times, you start to wonder if you can just write xyz. 😂

And in the continuing saga of questioning boilerplate, I wondered if I could get rid of render_with {} (which works due to copying local block variables down to the template) while at it.

So this is now possible in a dynamic Roda route:

---<%
name = "World"
%>---

Hello <%= name %>

Nice! 😌

Of course this will still work equally well:

---<%
render_with do
  name "World"
end
%>---

Hello <%= name %>

And regular front matter in static resources:

---
title: My Page
---

# <%= title %>

For folks who for whatever reason don't like this simplified syntax, the support_data_as_view_methods option can be set to false in the Bridgetown config, and then data.foo syntax is still mandatory.


For the Ruby nerds out there, the previous instance_eval code I'd written turned out to have a very strange side effect: even though the procs are eventually executed in the context of a Roda app instance, any modules/classes defined would actually be attached to the singleton class of Bridgetown::Routes::CodeBlocks. Weird, right? The likelihood of a file-based route needing to define a standalone module/class is low, but if it's supported it might as well work like any standard Ruby file being required at runtime. So I switched it up to use Kernel.eval and TOPLEVEL_BINDING instead of Bridgetown::Routes::CodeBlocks.instance_eval. Works! 😅

Also I finally got proper line number error reporting working as expected. This has been an ongoing frustration…turns out part of the reason why this has been inconsistent is our front matter regexps were swallowing a variable amount of newlines as whitespace around the template boundaries via \s. Changing that to [ \t] resolves the issue, and then calling lstrip restores the previous whitespace behavior after regexp parsing.

Copy link

render bot commented Apr 28, 2024

Copy link

render bot commented Apr 28, 2024

@jaredcwhite jaredcwhite added this to the 2.0 milestone Apr 28, 2024
@jaredcwhite jaredcwhite added SSR Dynamic rendering of Bridgetown pages enhancement New feature or request labels Apr 28, 2024
@jaredcwhite jaredcwhite merged commit 377c9ad into main May 4, 2024
3 checks passed
@jaredcwhite jaredcwhite deleted the simplify-data-syntax-and-roda-rendering branch May 4, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request SSR Dynamic rendering of Bridgetown pages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant