Shopify / Shopify/liquid

Liquid::Template options are not propagated on new isolated subcontext

Open
#1,853 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

Liquid::Context#new_isolated_subcontext should also set the render options strict_variables and strict_filters for the subcontext.

Maybe im missing something but I cant figure out how to raise undefined variable or undefined filter in partials using the render tag.

Please advise, happy to write a pr.

Example:

class PatchedContext < Liquid::Context
  def new_isolated_subcontext
    super.tap do |subcontext|
      subcontext.strict_variables = strict_variables
      subcontext.strict_filters = strict_filters
    end
  end
end

class FileSystem
  def read_template_file(_template_path)
    "{{ undefined_var }}{{ 'test' | undefined_filter }}"
  end
end

# normal example, no errors
liquid_context = Liquid::Context.build(registers: {file_system: FileSystem.new})
template = Liquid::Template.parse("{% render 'partial' %}")
template.render(liquid_context, strict_variables: true, strict_filters: true)
template.errors
# => []

# expected behavior, errors in partial
patched_context = PatchedContext.build(registers: {file_system: FileSystem.new})
template = Liquid::Template.parse("{% render 'partial' %}")
template.render(patched_context, strict_variables: true, strict_filters: true)
template.errors
# =>
# [#<Liquid::UndefinedVariable: Liquid error: undefined variable undefined_var>,
#  #<Liquid::UndefinedFilter: Liquid error: undefined filter undefined_filter>]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Liquid::Context#new_isolated_subcontext and inspect how the render options strict_variables and strict_filters reach a newly isolated subcontext. Reproduce the render-tag example with an undefined variable and filter in a partial, then verify that both errors are reported after the options are propagated.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.