lib/arbre/patches.rb in express_templates-0.11.11 vs lib/arbre/patches.rb in express_templates-0.11.13
- old
+ new
@@ -12,10 +12,11 @@
# we do not want to check the arity of the
# block in express templates because components
# are expected to be able to contain other components or template code
# without use of a builder style syntax
def build_tag(klass, *args, &block)
+ return if should_supress_output?(args)
tag = klass.new(arbre_context)
tag.parent = current_arbre_element
with_current_arbre_element tag do
begin
@@ -25,9 +26,25 @@
end
end
tag
end
+
+ # Conditionally do not emit markup
+ # Example that would supress generation of the h2 markup:
+ # * h2(only_when: false) { "Some text" }
+ # * h2(unless: true) { "Some text" }
+ def should_supress_output?(args)
+ if args.last.kind_of?(Hash)
+ only_when = args.last[:only_when]
+ unless_condition = args.last[:unless]
+ return true if only_when === false
+ return true if !unless_condition.nil? && !!unless_condition
+ else
+ false
+ end
+ end
+
def on_component_error(tag, exception)
tag.content = "Error rendering #{tag.class} component: #{exception.message}"
::Rails.logger.error exception
::Rails.logger.error exception.backtrace.slice(0..20).join("\n")