Created
August 20, 2015 14:51
-
-
Save joshnesbitt/971770c415e6405dadbe to your computer and use it in GitHub Desktop.
Example of a simple templating language written in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VariableLang | |
def initialize(path) | |
@path = path | |
@content = File.read(path) | |
end | |
def render(locals = {}) | |
locals.inject(@content.dup) do |buffer, vars| | |
key = vars.first | |
value = vars.last | |
buffer.gsub(/@#{key}/, value) | |
end | |
end | |
end | |
template = VariableLang.new('template.var') | |
content = template.render( | |
to: 'Josh', | |
service: 'Sassy', | |
from: 'Sassy Team' | |
) | |
puts content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello @to, | |
Thanks for signing up to @service! You'll love it here. | |
Cheers, | |
@from |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And if you wanted to validate all variables have been replaced: