2016-11-07 149 views
5

我正在寻找一种创建或者优雅的方式来设置系统,在那里我可以看到某些HTML内容来自哪里。我正在开发一个大型项目,我的很多时间都在确定某个HTML内容的来源。我知道你能够看到哪些布局,部分用于在日志中呈现页面,但我正在寻找更实用的东西。Rails描述HTML内容源

这将是一个例子。

<!-- ... app/views/layouts/main.html.slim --> 
<body> 
    <!-- ... app/views/people/index.html.slim --> 
    <div class="foo"> 
    <table clas="items"> 
     <!-- ... app/views/people/shared/_person.html.slim --> 
     <td> 
     <span>John Doe</span> 
     </td> 
    </table> 
    </div> 
</body> 

在呈现任何partial/page/layout rails之前渲染引擎会添加一条描述原点的注释。

+0

让我看看我是否明白这一点。您希望在呈现网页的html源代码中添加注释,其中包含指向您呈现的html的路由,每次都有一个要呈现的调用?这只会在你的开发环境中? – mlabarca

+0

如果我在每次渲染之前获得所需的要求,可以在文件路径中添加注释 –

+0

它不是基于配置的,但可以循环浏览app/views/*并在[shell脚本](http ://stackoverflow.com/questions/10587615/unix-command-to-prepend-text-to-a-file)。 – anyarms

回答

3

也许这rails_view_annotator宝石将是一个很好的起点:

Rails的视图标注器包装的Rails的谐音与指示渲染部分的磁盘位置HTML注释呈现。

根据项目的README,创业板将输出呈现的内容是这样的:

<!-- begin: app/views/user/_bio.html.haml (from app/views/user/show.html.haml:4) --> 
<div class='bio'>Ed's Bio</div> 
<!-- end: app/views/user/_bio.html.haml (from app/views/user/show.html.haml:4) --> 

但是,它看起来并不像它在积极发展,可能不兼容最新的Rails版本,因人而异。

+0

此宝石仅用于渲染偏色。它不会显示用于渲染视图的模板 – Aleks

+0

正确,这个gem只是这个问题的一个“部分”解决方案(参见https://github.com/duncanbeevers/rails_view_annotator/issues/4)。不过,它看起来仍然是一个很好的起点。 – wjordan

+0

谢谢,这有助于很多。当之无愧的赏金。 –

0

我不知道是否有这样的宝石,但是你可以重写设计用于渲染的全部位于看法和泛音输出类:

.bundle/ruby/ruby-2.3.1/gems/action-view-4.2.5.1/lib/action_view/template.rb(更换您的版本号)

有一种方法称为def compile(mod)不支持该方法来添加您想要在视图中呈现的自定义字符串。对于你的情况,你可以做这样的事情:

def compile(mod) #:nodoc: 
    encode! 
    method_name = self.method_name 
    code = @handler.call(self) 

    # This is the code I have added 
    code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;") 
    code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;") 

    # Make sure that the resulting String to be eval'd is in the 
    # encoding of the code 
    source = <<-end_src 
     def #{method_name}(local_assigns, output_buffer) 
     _old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code} 
     ensure 
     @virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer 
     end 
    end_src 

    # Make sure the source is in the encoding of the returned code 
    source.force_encoding(code.encoding) 

    # In case we get back a String from a handler that is not in 
    # BINARY or the default_internal, encode it to the default_internal 
    source.encode! 

    # Now, validate that the source we got back from the template 
    # handler is valid in the default_internal. This is for handlers 
    # that handle encoding but screw up 
    unless source.valid_encoding? 
     raise WrongEncodingError.new(@source, Encoding.default_internal) 
    end 

    mod.module_eval(source, identifier, 0) 
    ObjectSpace.define_finalizer(self, Finalizer[method_name, mod]) 
    end 

我加入的代码是:

code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;") 
code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;") 

它将在视图中的所有谐音,所有的观点的输出,因为他们正在渲染。

结果会是这样:

<!--app/views/profiles/_header.html.erb start --> 
Your text on the page 
<!--app/views/profiles/_header.html.erb end --> 

如果你想提取到一个单独的类,在config/initializers/目录中的Rails项目,叫做创建一个新的初始化,例如render_override.rb和粘贴代码的东西像这样:

ActionView::Template.class_eval do 
    # @override 
    def compile(mod) 
    encode! 
    method_name = self.method_name 
    code = @handler.call(self) 

    # This is the code I have added 
    code.insert(63, "@output_buffer.safe_append='\n<!--#{self.inspect} start -->\n'.freeze\;") 
    code.insert(code.size-19, "@output_buffer.safe_append='\n<!--#{self.inspect} end -->\n'.freeze\;") 

    # Make sure that the resulting String to be eval'd is in the 
    # encoding of the code 
    source = <<-end_src 
     def #{method_name}(local_assigns, output_buffer) 
     _old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code} 
     ensure 
     @virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer 
     end 
    end_src 

    # Make sure the source is in the encoding of the returned code 
    source.force_encoding(code.encoding) 

    # In case we get back a String from a handler that is not in 
    # BINARY or the default_internal, encode it to the default_internal 
    source.encode! 

    # Now, validate that the source we got back from the template 
    # handler is valid in the default_internal. This is for handlers 
    # that handle encoding but screw up 
    unless source.valid_encoding? 
     raise WrongEncodingError.new(@source, Encoding.default_internal) 
    end 

    mod.module_eval(source, identifier, 0) 
    ObjectSpace.define_finalizer(self, Finalizer[method_name, mod]) 
    end 
end 

重新启动服务器,这样的变化可以捡起来,和你的观点现在将这些新设置渲染。

你可能想用一个检查RAILS_ENV='dev'或类似的东西来包围你的eval类,这样它可以在开发中运行,并且你准备好了。