1 00:00:00,554 --> 00:00:04,390 I've added the plain Ruby method called page content to the app. 2 00:00:04,390 --> 00:00:06,750 You can type in the code you see here if you want. 3 00:00:06,750 --> 00:00:09,150 But if you launch a workspace from this video's page, 4 00:00:09,150 --> 00:00:11,820 you'll get a workspace with this method already set up for you. 5 00:00:12,840 --> 00:00:16,110 The page content method loads the contents of a text file and 6 00:00:16,110 --> 00:00:18,020 returns them as a string. 7 00:00:18,020 --> 00:00:21,230 You pass the title of a page to it as an argument and 8 00:00:21,230 --> 00:00:24,660 it uses the read method on the core Ruby file class to open 9 00:00:24,660 --> 00:00:29,630 a .txt file from a pages sub directory within your apps main directory. 10 00:00:29,630 --> 00:00:33,430 Check the teacher's notes if you'd like to know more about the file.read method. 11 00:00:33,430 --> 00:00:39,140 If the file isn't found, file.read will raise this Errno ENOENT exception. 12 00:00:39,140 --> 00:00:43,190 So we put this rescue clause here that will intercept that error if it happens 13 00:00:43,190 --> 00:00:44,160 and just return nil. 14 00:00:45,250 --> 00:00:47,790 So if the file isn't there, we just won't get anything back. 15 00:00:48,960 --> 00:00:52,930 Now we need the page's sub directory with text file for it to load from. 16 00:00:52,930 --> 00:00:54,260 So I'll create it in folder. 17 00:00:57,380 --> 00:00:58,370 Name it pages. 18 00:01:00,090 --> 00:01:01,926 And then create a new file within it. 19 00:01:04,679 --> 00:01:08,010 This file will hold a bio for one of our Treehouse teachers, Nick Pettit. 20 00:01:08,010 --> 00:01:11,524 So I'll name it Nick Pettit.txt. 21 00:01:14,231 --> 00:01:17,803 The file name has to end with a .txt extension because that's what the page 22 00:01:17,803 --> 00:01:20,430 content method will be looking for. 23 00:01:20,430 --> 00:01:27,460 For the file contents, I'll just put Treehouse teacher and game developer. 24 00:01:30,040 --> 00:01:33,910 Now let me copy this page content method to a new file all by itself, so 25 00:01:33,910 --> 00:01:35,910 I can show you how it works. 26 00:01:35,910 --> 00:01:39,170 We'll create a new file, name it test.rb. 27 00:01:41,290 --> 00:01:44,680 There's no need to require the Sinatra library since it's not used by any 28 00:01:44,680 --> 00:01:45,200 of this code. 29 00:01:46,270 --> 00:01:49,270 Now I'll add a call to the method and print the return value. 30 00:01:49,270 --> 00:01:56,930 So put this page content and we'll get an argument of 31 00:01:56,930 --> 00:02:02,190 Nick Pettit, that's the name of the text file without the txt extension. 32 00:02:03,450 --> 00:02:07,580 I didn't include the .txt on the end because page content adds that itself. 33 00:02:08,650 --> 00:02:10,930 If you launch the workspace that comes with this video, 34 00:02:10,930 --> 00:02:13,310 we'll ensure all that's set up for you. 35 00:02:13,310 --> 00:02:15,418 Now let's go to our console and try running this. 36 00:02:15,418 --> 00:02:21,260 Ruby space test.rb and they'll load the contents of the text file and print them. 37 00:02:22,460 --> 00:02:25,360 Don't worry if you don't understand every detail of how the page 38 00:02:25,360 --> 00:02:26,830 content method works. 39 00:02:26,830 --> 00:02:29,285 That's just what we're using for this particular app and 40 00:02:29,285 --> 00:02:31,890 it's not essential to understanding Sinatra. 41 00:02:31,890 --> 00:02:34,210 There's more info in the teacher's notes if you want it. 42 00:02:35,770 --> 00:02:39,350 We don't have any further need for the test our ,rb file right now. 43 00:02:39,350 --> 00:02:41,820 So I'm going to delete it from my workspace.