RAILS 压倒性视图渲染

发布于 2024-10-06 09:32:57 字数 3233 浏览 9 评论 0原文

我想覆盖 Rails 如何创建视图 *.html.erb

在 ActionView 包中我已经尝试过这样做。这样做

class ERB
  class Compiler # :nodoc:
..
 class Buffer # :nodoc:
  def compile(s)
   ...
   #It stores in a buffer each ruby chunk in the views inside of a Buffer.
  end
 end
   end
...
 # Here it is where is called compile method. 
 # The thing is that if my view is made up of several *.html.erb files such as partials this method will be invoked each time.        
        #INVOKED PER EACH html.erb file

 def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
     puts ">>> initialize"    
     @safe_level = safe_level
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => "%d %b %Y")
     compiler = ERB::Compiler.new(trim_mode)
    # raise "need a block" 
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => nil)
     set_eoutvar(compiler, eoutvar)
     @src = compiler.compile(str)  
     @filename = nil
   end
 end
end

我想知道底部的过程何时开始。我的意思是我必须覆盖哪个类、文件等才能查看 Rails 开始为特定视图调用所有 html.erb 的位置。

我认为我应该在这里:

require 'delegate'
require 'optparse'
require 'fileutils'
require 'tempfile'
require 'erb'

module Rails
  module Generator
    module Commands

        class Create 

              # Generate a file for a Rails application using an ERuby template.
              # Looks up and evaluates a template by name and writes the result.
              #
              # The ERB template uses explicit trim mode to best control the
              # proliferation of whitespace in generated code.  <%- trims leading
              # whitespace; -%> trims trailing whitespace including one newline.
              #
              # A hash of template options may be passed as the last argument.
              # The options accepted by the file are accepted as well as :assigns,
              # a hash of variable bindings.  Example:
              #   template 'foo', 'bar', :assigns => { :action => 'view' }
              #
              # Template is implemented in terms of file.  It calls file with a
              # block which takes a file handle and returns its rendered contents.
              def template(relative_source, relative_destination, template_options = {})
                puts "EEEEEEEEEEEEEEEEEEEEEe"
                file(relative_source, relative_destination, template_options) do |file|
                  # Evaluate any assignments in a temporary, throwaway binding.
                  vars = template_options[:assigns] || {}
                  b = template_options[:binding] || binding
                  vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }

                  # Render the source file with the temporary binding.
                  ERB.new(file.read, nil, '-').result(b)
                end
             end

        end

    end
  end
end

但我没有从 put 方法中找到任何痕迹。

所有重命名都放在一个名为 /lib/*_extensions.erb 的文件中,在 /config/initializers/extensions.rb 中我有下一个:

Dir[File.dirname(__FILE__) + "/../../lib/*_extensions.rb"].each do |fn|
  require fn
end

我不想透露为什么我要这样做。

谢谢

I want to override how rails creates a view *.html.erb

In ActionView package I already Tried to do it. Doing it

class ERB
  class Compiler # :nodoc:
..
 class Buffer # :nodoc:
  def compile(s)
   ...
   #It stores in a buffer each ruby chunk in the views inside of a Buffer.
  end
 end
   end
...
 # Here it is where is called compile method. 
 # The thing is that if my view is made up of several *.html.erb files such as partials this method will be invoked each time.        
        #INVOKED PER EACH html.erb file

 def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
     puts ">>> initialize"    
     @safe_level = safe_level
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => "%d %b %Y")
     compiler = ERB::Compiler.new(trim_mode)
    # raise "need a block" 
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => nil)
     set_eoutvar(compiler, eoutvar)
     @src = compiler.compile(str)  
     @filename = nil
   end
 end
end

I would like to find out when the process on the bottom starts. I mean which class, file, etc I have to override to see where Rails starts invoking all html.erb for an specific view.

I think that I should be here:

require 'delegate'
require 'optparse'
require 'fileutils'
require 'tempfile'
require 'erb'

module Rails
  module Generator
    module Commands

        class Create 

              # Generate a file for a Rails application using an ERuby template.
              # Looks up and evaluates a template by name and writes the result.
              #
              # The ERB template uses explicit trim mode to best control the
              # proliferation of whitespace in generated code.  <%- trims leading
              # whitespace; -%> trims trailing whitespace including one newline.
              #
              # A hash of template options may be passed as the last argument.
              # The options accepted by the file are accepted as well as :assigns,
              # a hash of variable bindings.  Example:
              #   template 'foo', 'bar', :assigns => { :action => 'view' }
              #
              # Template is implemented in terms of file.  It calls file with a
              # block which takes a file handle and returns its rendered contents.
              def template(relative_source, relative_destination, template_options = {})
                puts "EEEEEEEEEEEEEEEEEEEEEe"
                file(relative_source, relative_destination, template_options) do |file|
                  # Evaluate any assignments in a temporary, throwaway binding.
                  vars = template_options[:assigns] || {}
                  b = template_options[:binding] || binding
                  vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }

                  # Render the source file with the temporary binding.
                  ERB.new(file.read, nil, '-').result(b)
                end
             end

        end

    end
  end
end

But I do not find any trace from the puts method.

All renaming are placed in a file called /lib/*_extensions.erb and in /config/initializers/extensions.rb I have the next:

Dir[File.dirname(__FILE__) + "/../../lib/*_extensions.rb"].each do |fn|
  require fn
end

I do not want to reveal why I am doing this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-10-13 09:32:57

我想使用 before_render 它至少在 Rails 2.3.4 中不存在。

所以如果你想这样做,我就这样做了:

    class ApplicationController < ActionController::Base
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details

      # Scrub sensitive parameters from your log
      # filter_parameter_logging :password

      protected
      def render(options = nil, extra_options = {}, &block) #:doc:
        puts "BEFORE render"
        puts "Setting %H:%M:%S"
        ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%H:%M:%S")
        # call the ActionController::Base render to show the page
        super
       puts "AFTER render"
       puts "Resetting"
       ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => nil)
      end

end

I wanted to use a before_render it does not exist at least in Rails 2.3.4.

So if you want to do it, I did as the next:

    class ApplicationController < ActionController::Base
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details

      # Scrub sensitive parameters from your log
      # filter_parameter_logging :password

      protected
      def render(options = nil, extra_options = {}, &block) #:doc:
        puts "BEFORE render"
        puts "Setting %H:%M:%S"
        ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%H:%M:%S")
        # call the ActionController::Base render to show the page
        super
       puts "AFTER render"
       puts "Resetting"
       ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => nil)
      end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文