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

Add concept of Liquid::Environment #1815

Merged
merged 2 commits into from
Aug 7, 2024
Merged

Add concept of Liquid::Environment #1815

merged 2 commits into from
Aug 7, 2024

Conversation

ianks
Copy link
Contributor

@ianks ianks commented Jul 17, 2024

Currently, Liquid relies heavily on global state which makes it pretty inflexible. It's common to see code unsafely mutating global tags with Liquid::Template.register_tag. This PR offers a way to configure multiple sandboxed Liquid rendering environments.

Liquid::Environment

In Liquid, an "Environment" is a scoped environment that encapsulates custom tags, filters, and other configurations. This allows you to define and isolate different sets of functionality for different contexts, avoiding global overrides that can lead to conflicts and unexpected behavior.

By using Worlds, you can:

  1. Encapsulate Logic: Keep the logic for different parts of your application separate.
  2. Avoid Conflicts: Prevent custom tags and filters from clashing with each other.
  3. Improve Maintainability: Make it easier to manage and understand the scope of customizations.
  4. Enhance Security: Limit the availability of certain tags and filters to specific contexts.

Here's an example of how you can define and use Worlds in Liquid:

user_env = Liquid::Environment.build do |env|
  env.register_tag("renderobj", RenderObjTag)
end

Liquid::Template.parse(<<~LIQUID, environment: user_env)
  {% renderobj src: "path/to/model.obj" %}
LIQUID

In this example, RenderObjTag is a custom tag that is only available within the user_world.

Similarly, you can define another world for a different context, such as email templates:

email_env = Liquid::Environment.build do |env|
  env.register_tag("unsubscribe_footer", UnsubscribeFooter)
end

Liquid::Template.parse(<<~LIQUID, world: email_env)
  {% unsubscribe_footer %}
LIQUID

@tobi
Copy link
Member

tobi commented Jul 20, 2024

World isn't the right title here, It's more like Session or Environment. But the concept is sound

@ianks
Copy link
Contributor Author

ianks commented Jul 22, 2024

World isn't the right title here, It's more like Session or Environment. But the concept is sound

Agree, renamed to Environment in 58cef46

@ianks ianks changed the title Add concept of Liquid::World Add concept of Liquid::Environment Jul 22, 2024
@ianks ianks requested a review from ggmichaelgo August 1, 2024 20:10
@ianks ianks merged commit fb6634f into main Aug 7, 2024
11 checks passed
@ianks ianks deleted the liquid-worlds branch August 7, 2024 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants