# frozen_string_literal: true # plugins should extend this module DefaultHelper end class Lux::Helper class << self def for group, locals={}, &block base_class_helper = if [Symbol, String].include?(group.class) "#{group}_helper".classify.constantize elsif group group end helper = Lux::Helper.new helper.extend DefaultHelper helper.extend base_class_helper if base_class_helper # locals overide globals for k, v in locals helper.instance_variable_set("@#{k.to_s.sub('@','')}", v) end return helper.instance_exec &block if block helper end end def no_white_space yield.gsub(/>\s+,'><') end def page Lux.page end def request Lux.page.request end def params Lux.page.params end # renders just template but it is called # = render :_link, link:link # = render 'main/links/_link', link:link def render name, locals={} if name.is_array? return name.map { |b| render(b) }.join("\n") elsif name.respond_to?(:db_schema) path = Lux.thread[:last_template_path].split('/')[1] table_name = name.class.name.tableize locals[table_name.singularize.to_sym] = name eval "@_#{table_name.singularize} = name" name = "#{path}/#{table_name}/_#{table_name.singularize}" else name = name.to_s name = "#{Thread.current[:lux][:last_template_path]}/#{name}" unless name.index('/') end ivh = instance_variables_hash for k, v in locals ivh["@_#{k}"] = v end if block_given? name = "#{name}/layout" unless name.index('/') Lux::Template.new(name, ivh).render_part { yield } else Lux::Template.new(name, ivh).render_part end end def cache *args, &block ttl = args.last.class == Hash ? args.pop[:ttl] : nil key = 'view:'+Lux.cache.generate_key(args)+block.source_location.join(':') Lux.cache.fetch(key, ttl) { yield } end def error msg %[
#{msg}] end # = render 'main/tag_list', klass:Link # = cell Main::PartsCell, :tag_filter, klass def cell name, action, *args klass = name.is_string? ? "#{name}_cell".classify.constantize : name klass.cell(action, *args) rescue error("#{klass}.#{action}()\n\n#{$!.message}") end # add locale prefix if using locale def localize path return Lux.config(:localize) ? "/#{Locale.current}#{path}" : path end # tag :div, { 'class'=>'iform' } do def tag name=nil, opts={}, data=nil return Lux::Helper::HtmlTag unless name data = yield(opts) if block_given? Lux::Helper::HtmlTag.tag name, opts, data end end