E
E
Evgeny Kungurov2015-10-31 12:29:08
Ruby on Rails
Evgeny Kungurov, 2015-10-31 12:29:08

Self-written rails. How to execute a method inside a view?

Hello. I'm trying to make my own MVC framework similar to rails, I need it for self-development. I implemented the base controller and for each view controller, everything is the same as in rails. I did a job with the general template through yield, but I can't figure out how to pass the method inside the template? For example, I have a main template and everything is divided into blocks, let's say the block <%= render : head %> below is yield to insert a template based on the name of the controller and action.
So far done like this:

require 'erubis'

module LiveQueue
  class Controller
    def render(views_action, options = {})
      template = File.read(File.join('app', 'views', controller_name, "#{views_action}.html.erb"))
      Erubis::Eruby.new(template).result(options)
    end

    def default_template(&block)
      template = File.read(File.join('public', 'application.html.erb'))
      Erubis::Eruby.new(template).evaluate(&block)
    end

    def render_layout(name)
      template = File.read(File.join('app', 'views', 'layouts', "_#{name}.html.erb"))
    end

    private

    def controller_name
      self.class.to_s.gsub(/Controller$/, "").downcase
    end
  end
end

This is the gem code, and already in the application
class AdminController < LiveQueue::Controller
  def index
    default_template { render(:index, name: 'Евгений', last_name: 'Кунгуров') }
  end
end

The implementation of render looks a little redundant and most likely it is not the way to do it, because we have to transfer our view to the main template, how to refactor, I guess, we get one render method, and if we don’t need the main template, I’ll do something like render default_temp: none.
In the console I tried to do it manually like this:
template = "привет <%= render %>"  
def  render;  'еще раз привет';  end
Erubis::Eruby.new(template).evaluate { render }
Так метод передается.

So far, with this code, I tried to do everything differently, given the example from the console, I tried it like this
def default_template(&block)
  template = File.read(File.join('public', 'application.html.erb'))
  Erubis::Eruby.new(template).evaluate(&block) { метод, который надо передать }
end

But still NoMethod swears at my method in Erubis::Context.
Am I doing it wrong at all? Even if it worked out, I would have to pass all my helpers to views. I watched Action View, a whole framework where I can implement all this, at least do it. Rummaged did not understand how it is done.
All the examples on the net are mostly related to yield and passing variables

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Kungurov, 2015-11-02
@evgenykungurov

In general, I solved the problem, as I guessed, I extended the Context class of the Erubis module.
When we do Erubis::Eruby.new(template).evaluate, the evaluate method creates an instance of Context.new, and since I extended it with my own methods, everything worked

module Erubis
  class Context
     include MyHelpers
  end
end

Thanks for the support, poking around in the sources is a useful and interesting thing, now my horizons have expanded well.
Here is also a link to an interesting blog on how rendering works
in rails. 2 methods helper, helper_method are responsible for access to views in rails.
Maybe it will be useful to someone)

O
OnYourLips, 2015-10-31
@OnYourLips

but I can't figure out how to pass the method inside the template?

Why don't you see how this is done in rails?
Writing something of your own before studying analogues is the last thing (including for learning: without studying analogues, you don’t understand how to do it right).

C
CapeRatel, 2015-11-01
@CapeRatel

I'll give my opinion, nothing more.
Man you are great. This is a very cool practice. Everyone should write their own great to understand how the language works, how frames work.
BUT:
if they start dismantling "your rails" for you, then nothing good will come of it.
Tip:
Try each stage of the logic to check and go through all the files. Also, each link was googled on the docks and figured out how it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question