Sinatra Views (lab)
Last updated
Last updated
We'll explore the purpose of views in a Sinatra application and will render them as separate files.
Explain the advantage of storing HTML in a separate file from app.rb
Create index.erb
in the views directory
Update your controller to render appropriate erb files
Render multiple routes with multiple views
Rendering plain text is a great way to test the behaviors of our routes, but it doesn't give us any control over how the content is displayed. We'd like to structure our content using HTML and render that to the browser instead. The most basic way to do this is to include html tags as a part of the string we're rendering. If you haven't already, fork, clone, and open this lab. In the "/" route of our app.rb
file, try the following:
Run shotgun
and go to or the IP Address:PORT provided by the Learn IDE. Your "Hello World" text should now appear as an <h1>
. Nice!
You can imagine that writing HTML this way would get very messy, very quickly. Luckily, most web frameworks include a templating engine that allows us to render HTML content from a different file. We call these files "views." Views represent what the user sees and comprise the "V" in "MVC."
By default, Sinatra uses a templating engine called ERB, or Embedded RuBy. Our view files will use the extension .erb
and we can write HTML code there just like in a plain old .html
file. Sinatra is also configured by default to look for our .erb
files in a directory called views
.
Create a new file called index.erb
inside of the views
directory. Add the following code into that file.
Now, we just need to update our controller to render the index.erb
file at the "/" route. The syntax for this is as follows:
This tells Sinatra to render a file called index.erb
inside of a directory called views
. Save your files and refresh your preview to see your changes. Awesome, right?
We can create as many routes and views as we want. Let's create a route called "/info" in our controller.
Finally, update our controller to render that file.
It's important to note that the name of the file doesn't have to match the name of the route. For example, if we wanted our "/info" route to render a file called dogs.erb
, we could do so like this:
By convention though, we keep our routes and our erb files named the same. This makes it easier to keep track as our projects get bigger.
With shotgun
running, head to . You should see "Testing the info page" rendered there. This lets us know that our route is defined properly. Next, let's have this route render a separate file instead. Inside of the views
directory, create a file called info.erb
. Add the following code into that file, and whatever else you like.
Clone this repo here: