codereading / sinatra

Classy web-development dressed in a DSL (official / canonical repo)
http://www.sinatrarb.com/
MIT License
12 stars 2 forks source link

How do "inline templates" work internally? #7

Open ericgj opened 12 years ago

ericgj commented 12 years ago

@codereading/readers

ericgj commented 12 years ago

The Sinatra docs say:

NOTE: Inline templates defined in the source file that requires sinatra are automatically loaded. Call enable :inline_templates explicitly if you have inline templates in other source files.

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 enable :inline_templates . Curious to see that you could also do something like set :inline_templates, 'another_file.rb' .

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
kgrz commented 12 years ago

Inline templates essentially are written after the entire app is written. For example:

require 'sinatra'
  get '/' do
    erb :index
  end
__END__
@@ index
<html>
 <!-- Some stuff here -->
</html>

A complete example can be found in the chat application under sinatra/examples folder

This is what I understood:

lyonsinbeta commented 12 years ago

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.