lib/hud.rb in hud-2.3.0 vs lib/hud.rb in hud-2.4.0

- old
+ new

@@ -1,9 +1,10 @@ require "rack/app" require "yaml" require "tilt" require "pry" +require "oga" require "ostruct" require "tilt/erb" require "rack/app/front_end" require_relative "hud/version" @@ -81,13 +82,13 @@ end class Display module Helpers - def display(name, from: nil, locals: {}) + def display(name, from: nil, locals: {},css: nil) klz = Display.build(name) - klz.call(locals: locals).render_template(name: name, locals: @locals, from: from) + klz.call(locals: locals).render_template(name: name, locals: @locals, from: from,css: css) end alias_method :render, :display alias_method :d, :display alias_method :r, :display end @@ -112,13 +113,15 @@ def development? ENV["RACK_ENV"] == "development" end - def display(name, locals: {}) + def display(name, locals: {},css:nil) template = Tilt::ERBTemplate.new("#{Hud.configuration.base_path}/components/#{name.to_s}.html.erb") - template.render(self, locals) + result = template.render(self, locals) + return Oga.parse_html(result).css(css) if css + result end alias_method :render, :display alias_method :d, :display alias_method :r, :display @@ -133,11 +136,11 @@ def self.call(locals: {}) new(locals: locals) end - def render_template(name: nil, from: nil, locals: {}) + def render_template(name: nil, from: nil, locals: {},css: nil) name ||= self.class.to_s.downcase.gsub("::", "_") base_path = Pathname.new(Rack::App::Utils.pwd) paths_to_check = [] @@ -153,13 +156,17 @@ if File.exist?(path) template = Tilt::ERBTemplate.new(path) puts path if from.nil? - return template.render(self, locals) + result = template.render(self, locals) + return Oga.parse_html(result).css(css) if css + return result else from_path = base_path.join(from, "components") - return template.render(self, locals) if path.to_path.start_with? from_path.to_s + result = template.render(self, locals) + return Oga.parse_html(result).css(css) if css + return result if path.to_path.start_with? from_path.to_s end end end