Class: MailchimpTemplate
- Inherits:
-
Object
- Object
- MailchimpTemplate
- Defined in:
- lib/mailchimp_template.rb,
lib/mailchimp_template/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
-
#initialize(template) ⇒ MailchimpTemplate
constructor
A new instance of MailchimpTemplate.
- #merge_tags ⇒ Object
- #render(merge_tags: {}, regions: {}) ⇒ Object
Constructor Details
#initialize(template) ⇒ MailchimpTemplate
Returns a new instance of MailchimpTemplate.
6 7 8 |
# File 'lib/mailchimp_template.rb', line 6 def initialize(template) @template = template end |
Instance Method Details
#merge_tags ⇒ Object
42 43 44 |
# File 'lib/mailchimp_template.rb', line 42 def @template.scan(/\*\|(?<tag_name>.+?)\|\*/).flatten end |
#render(merge_tags: {}, regions: {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mailchimp_template.rb', line 10 def render(merge_tags: {}, regions: {}) html = Nokogiri::HTML::DocumentFragment.parse(@template) html.css("*").select { |n| n.attr("mc:edit") }.each do |editable_region| editable_region.children = Nokogiri::HTML::DocumentFragment.parse(regions[editable_region.attr("mc:edit")]) editable_region.remove_attribute("mc:edit") end result = html.to_s result.gsub! /\*\|(?<tag_name>.+?)\|\*/ do |match| case $~[:tag_name] when /^IF:(?<cond>.+)/, /^IFNOT:(?<cond>.+)/, /^ELSEIF:(?<cond>.+)/, "ELSE:", "END:IF" #TODO: handle conditional merge tags match when "MC:TOC" #TODO: generate TOC of h1 and h2 tags match when "MC:TOC_TEXT" #TODO: generate TOC of h1 and h2 tags, render as text match when /^DATE:(?<format>.+)/ #TODO: render with PHP date format (nontrivial, requires a custom parser) match when "CURRENT_YEAR" Date.today.year else [$~[:tag_name]] || match end end return result end |