-
-
Save OpenCoderX/4459566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'action_view' | |
module ActionView | |
module Template::Handlers | |
class NokogiriBuilder | |
class_attribute :default_format | |
self.default_format = Mime::XML | |
def call(template) | |
"xml = ::Nokogiri::XML::Builder.new { |xml|" + | |
template.source + | |
"}.to_xml;" | |
end | |
protected | |
def require_engine | |
@required ||= begin | |
require "nokogiri" | |
true | |
end | |
end | |
end | |
end | |
end | |
ActionView::Template.register_template_handler :nokogiri, ActionView::Template::Handlers::NokogiriBuilder.new | |
#Placing files like this in the lib directory is sufficient. If you have many builders(handlers), then adding a lib/builders/ or lib/handlers/ directory may be better. Note that you'll need to add those paths to your application config with something like: | |
#add this to application.rb This will add all subdirectories in lib to your application auto load path. You could just add the specific paths you want to require. | |
config.autoload_paths += Dir[ "#{config.root}/lib/**/" ] | |
#The template file extension is your choice. In this gist, NokogiriBuilder.new is registered to handle files with the extension :nokogiri |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment