Skip to content

Instantly share code, notes, and snippets.

@mkrogh
Created September 1, 2014 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkrogh/95b9e53eba98f800361e to your computer and use it in GitHub Desktop.
Save mkrogh/95b9e53eba98f800361e to your computer and use it in GitHub Desktop.
A simple template for smaller template files.
class SimpleTemplate
attr_accessor :token_start, :token_end
def initialize(file)
@content = open(file).read
@token_start = "{{"
@token_end = "}}"
@token_status = :on
end
def replace(token,content)
@content.gsub!(wrap_token(token), content)
self
end
def replace_once(token,content)
@content.sub!(wrap_token(token), content)
self
end
def token(status)
@token_status = status
self
end
def save_file(file_name)
open(file_name, "w") do |file|
file.write(@content)
end
end
def build
@content
end
private
def wrap_token(token)
if @token_status == :on
/#{@token_start}#{token}#{@token_end}/
else
/#{token}/
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment