Skip to content

Instantly share code, notes, and snippets.

@krusty
Created September 21, 2012 19:25
Show Gist options
  • Save krusty/3763386 to your computer and use it in GitHub Desktop.
Save krusty/3763386 to your computer and use it in GitHub Desktop.
# base class for cached templates from the database
# each template has to be uniquely identified by its _name_ and _language_
module CachedTemplate
# get a template _name_ in _language_
# if the template can not be found in the right language,
# use english and any language as fallback
def self.find_by_language_and_name(klass,_language, _name)
[_language, "en", nil ].each do |lang|
conditions = {:name => _name}
conditions[:language] = lang if lang
template = klass.find(:first, :conditions => conditions)
return template if template.respond_to?(:render)
end
raise "Template #{_name} in language #{_language} not found."
end
# render the template with _local_assigns_ as instance variables
def render(local_assigns = {})
b=binding
local_assigns.each { |k,v| instance_variable_set(:"@#{k.to_s}",v) }
ERB::new(body).result(b)
end
def render_attribute(attribute, local_assigns = {})
local_assigns.each { |k,v| instance_variable_set(:"@#{k.to_s}",v) }
ERB::new(self.send(attribute)).result(binding)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment