-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do "inline templates" work internally? #7
Comments
The Sinatra docs say:
This is a puzzler: where do the inline templates defined in the source file that requires sinatra get loaded? Here's the code that runs when you https://github.com/codereading/sinatra/blob/ver1.3.2/lib/sinatra/base.rb#L1049 # Load embeded templates from the file; uses the caller's __FILE__
# when no file is specified.
def inline_templates=(file=nil)
file = (file.nil? || file == true) ? (caller_files.first || File.expand_path($0)) : file
begin
io = ::IO.respond_to?(:binread) ? ::IO.binread(file) : ::IO.read(file)
app, data = io.gsub("\r\n", "\n").split(/^__END__$/, 2)
rescue Errno::ENOENT
app, data = nil
end
if data
if app and app =~ /([^\n]*\n)?#[^\n]*coding: *(\S+)/m
encoding = $2
else
encoding = settings.default_encoding
end
lines = app.count("\n") + 1
template = nil
force_encoding data, encoding
data.each_line do |line|
lines += 1
if line =~ /^@@\s*(.*\S)\s*$/
template = force_encoding('', encoding)
templates[$1.to_sym] = [template, file, lines]
elsif template
template << line
end
end
end
end |
Inline templates essentially are written after the entire app is written. For example:
A complete example can be found in the chat application under sinatra/examples folder This is what I understood:
|
I've never seen inline erb before, online inline HAML. This is good to know for when you're writing really small apps, and don't want to use HAML. |
@codereading/readers
The text was updated successfully, but these errors were encountered: