slim-template / slim

Slim is a template language whose goal is to reduce the syntax to the essential parts without becoming cryptic.
https://slim-template.github.io
MIT License
5.3k stars 501 forks source link

Cannot call method defined in ruby: #952

Closed LevitatingBusinessMan closed 4 months ago

LevitatingBusinessMan commented 4 months ago
ruby:
  def a_unless_current
    @page_current ? {tag: 'span'} : {tag: 'a', href: 'https://slim-template.github.io/'}
  end
- @page_current = true
*a_unless_current Link
- @page_current = false
*a_unless_current Link

This example from the documentation does not seem to work.

It returns

t.slim:7:in `__tilt_620': undefined local variable or method `a_unless_current' for #<Object:0x000056c9e40ad598 @page_current=true> (NameError)
        from /home/rein/.gem/ruby/3.0.0/gems/tilt-2.3.0/lib/tilt/template.rb:207:in `bind_call'
        from /home/rein/.gem/ruby/3.0.0/gems/tilt-2.3.0/lib/tilt/template.rb:207:in `evaluate'
        from /home/rein/.gem/ruby/3.0.0/gems/tilt-2.3.0/lib/tilt/template.rb:102:in `render'
        from /home/rein/.gem/ruby/3.0.0/gems/slim-5.2.1/lib/slim/command.rb:104:in `process'
        from /home/rein/.gem/ruby/3.0.0/gems/slim-5.2.1/lib/slim/command.rb:20:in `run'
        from /home/rein/.gem/ruby/3.0.0/gems/slim-5.2.1/bin/slimrb:6:in `<top (required)>'
        from /home/rein/.gem/ruby/3.0.0/bin/slimrb:25:in `load'
        from /home/rein/.gem/ruby/3.0.0/bin/slimrb:25:in `<main>'

when executed with slimrb.

I haven't been able to call a method defined in a ruby: block at all so far.

LevitatingBusinessMan commented 4 months ago

One bypass I found is to assign a lambda instead.

LevitatingBusinessMan commented 4 months ago

Perfect, thank you!