Coder Social home page Coder Social logo

sinatra-views's Introduction

Sinatra Views

Overview

We'll explore the purpose of views in a Sinatra application and will render them as separate files.

Objectives

  1. Explain the advantage of storing HTML in a separate file from app.rb
  2. Create index.erb in the views directory
  3. Update your controller to render appropriate erb files
  4. Render multiple routes with multiple views

Part 1: Rendering HTML

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:

get '/' do
  "<h1>Hello World</h1>"
end

Run shotgun and go to http://localhost:9393. Your "Hello World" text should now appear as an <h1>. Nice!

Part 2: Using an ERB File

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.

<!DOCTYPE html>
<html>
  <head>
    <title>My First View!!</title>
  </head>
  <body>
    <h1>Hello World</h1>
    <p>This HTML code is inside of a '.erb' file in the views directory, where it belongs.</p>
  </body>
</html>

Now, we just need to update our controller to render the index.erb file at the "/" route. The syntax for this is as follows:

get '/' do
  erb :index
end

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?

Part 3: Multiple Routes with Multiple Views

We can create as many routes and views as we want. Let's create a route called "/info" in our controller.

get '/' do
  erb :index
end

get "/info" do
  "Testing the info page"
end

With shotgun running, head to http://localhost:9393/info . 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.

<!DOCTYPE html>
<html>
  <head>
    <title>Info Page</title>
  </head>
  <body>
    <h1>Info Page</h1>
    <p>This is the info page: here's some information about me!</p>
  </body>
</html>

Finally, update our controller to render that file.

  get '/' do
    erb :index
  end

  get "/info" do
    erb :info
  end

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:

  get '/' do
    erb :index
  end

  get "/info" do
    erb :dogs
  end

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.

sinatra-views's People

Contributors

annjohn avatar curiositypaths avatar dependabot[bot] avatar franknowinski avatar gj avatar ihollander avatar ipc103 avatar lizbur10 avatar maxwellbenton avatar plai217 avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinatra-views's Issues

There's a misleading sentence in the README

Hi. Let me preface this by saying that I may be nitpicking here, so please feel free to take it or leave it. I noticed that in Part 3 of this lab, there's a sentence that says, "Add whatever HTML code you like." Then, it shows the following HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Info Page</title>
    </head>
    <body>
        <h1>Info Page</h1>
        <p>This is the info page: here's some information about me!</p>
    </body>
</html>

I think the sentence that I mentioned needs to be changed to something like "Add the following code into that file, and whatever else you like:".

My reason for this is that the tests won't pass without that specific HTML, contrary to what the original sentence implies.

Thanks for looking into this!
Sdcrouse

Consider changing "hard coded" local host 9393

Is http://localhost:9393/ the most likely location of any user's shotgun sample? Would it be better to point the user to the shotgun command's output for the location? E.g. here's what my output was:

[14:52:58] (master) sinatra-views-cb-000 // โ™ฅ shotgun == Shotgun/Thin on http://138.68.11.226:30000/ Thin web server (v1.6.3 codename Protein Powder) Maximum connections set to 1024 Listening on 138.68.11.226:30000, CTRL+C to stop

Duplicate text in the README

Hi. It looks like the section "Part 3: Multiple Routes with Multiple Views" repeats a few sentences that were in "Part 1: Rendering HTML". In Part 3, you have this paragraph:

"Run shotgun and go to http://localhost:9393 or the IP Address:PORT provided by the Learn IDE. Your "Hello World" text should now appear as an <h1>. Nice! With shotgun running, head to http://localhost:9393/info . 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 whatever HTML code you like."

The part of the paragraph that I bolded and italicized is from Part 1 and is not needed in Part 3.

Thanks for looking into this!
Sdcrouse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.