Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Matthew McGarvey
Matthew McGarvey

Posted on

     

Crystal View Context

This article is meant to be more of a public note than anything.

I have found myself wondering why several Ruby gems that allowed you to create HTML in plain ruby still required you to have separate files instead of putting them in plain ol' ruby objects. As I'm working on my Crystal implementation of Roda, it finally occurred to me why. It's to be able to manage scope. All of those languages didn't require calling the methodson anything, the methods were just there. It's because the file is run in the context of a class that provides those messages.

Now why did I put Crystal in the title but only talk about Ruby? Well, we have a context issue in Crystal and we've struggled with it a fair amount. There's ECR which does some fancy macro things but ultimately just copy/pastes the ECR file directly into the place whereECR.render is called. Over in Lucky, though, we make HTML through Crystal code and they are separate objects. One of the pains of that is passing in the HTTP context for things like link helpers and CSRF tokens. Buuuut, if the Crystal code was in a separate, non-Crystal file, we could still have all the functionality of Lucky's html building, but with the context control of ECR.

I am sticking with ECR right now in my library, but I might experiment more in the future. One advantage of this over ECR is that I don't have to do any pre-processing to pull out the fancy tags for embedding crystal into regular HTML.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Just a dev who enjoys programming in Ruby and Crystal
  • Joined

More fromMatthew McGarvey

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp