Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access Middleman template helpers when extending a class in config.rb?

Tags:

ruby

middleman

This config.rb works:

helpers do
    def link_to_nothing(text)
        link_to(text, "#")
    end
end

With template index.html.erb:

<%= link_to_nothing "Test link" %>


But when I try to add a method to the Middleman::Sitemap::Resource class in this config.rb:

helpers do
    class Middleman::Sitemap::Resource
        def link(text)
            link_to(text, path)
        end
    end
end

With template index.html.erb:

<%= current_page.link "This page" %>

The following error comes up when loading the page:

NoMethodError at /index.html

undefined method `link_to' for #<Middleman::Sitemap::Resource:0x3139848>

like image 531
Spencer Avatar asked Jan 27 '26 11:01

Spencer


1 Answers

I've found that link_to is an instance method of class Middleman::Application, which I can access through the app variable:

helpers do
    class Middleman::Sitemap::Resource
       def link(text)
            app.link_to(text, path)
        end
    end
end
like image 125
Spencer Avatar answered Jan 30 '26 00:01

Spencer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!