Call Sinatra erb from another class

I need to display the erb Sinatra pattern inside my controller class. I have problems with this. I looked through the rdocs of Sinatra and came up with this:

Sinatra::Templates.erb :template_to_render 

When I do this, I get the following error:

 undefined method `erb' for Sinatra::Templates:Module 

Is there a way to call this from another class?

+7
ruby sinatra
source share
3 answers

To simulate the rendering behavior of a Sinatra controller in some other class (not a controller), you can create a module as follows:

 module ErbRender include Sinatra::Templates include Sinatra::Helpers include Sinatra::ContentFor def settings @settings ||= begin settings = Sinatra::Application.settings settings.root = "#{ROOT}/app" settings end end def template_cache @template_cache ||= Tilt::Cache.new end end 

Here you may need to configure settings.root

Usage example:

 class ArticleIndexingPostBody include ErbRender def get_body erb :'amp/articles/show', layout: :'amp/layout' end end 

This will correctly display layout templates, including content_for

+1
source share

why don't you require "erb" and after using only erb

 ## You'll need to require erb in your app require 'erb' get '/' do erb :index end 
0
source share

Perhaps your class will return the template name and display it in the main application.

Of course, this is not quite the answer (I do not have enough comments to add a comment to this account), and you are probably doing it now ...

0
source share

All Articles