Question: Can I get inline template as string in Sinatra?

Question

Can I get inline template as string in Sinatra?

Answers 1
Added at 2017-01-04 18:01
Tags
Question

Motivation

The inline template in Sinatra is very convenient (for me).

If it is possible to extract there contents as string, more applications can be expected.

Example

showing my intention in this code

require "sinatra"
require "sinatra/json"
require "yaml"

get "/" do
  content_type :json
  data = YAML.load inline_template_as_raw_string(:my_yaml)
  json data
end

__END__
@@my_yaml
foo:
  bar: baz

@@my_something
hello

any ideas?

Answers
nr: #1 dodano: 2017-01-04 19:01

Sinatra stores the raw loaded templates in a settings variables called templates. You can access them as follows:

require "sinatra"
require "sinatra/json"
require "yaml"

get "/" do
  content_type :json
  data = YAML.load settings.templates[:my_yaml][0]
  json data
end

__END__
@@my_yaml
foo:
  bar: baz

@@my_something
hello

settings.templates contains a Hash with the template names as keys and an array containing the raw template and the line number where it was defined.

Source Show
◀ Wstecz