1 00:00:00,740 --> 00:00:05,280 In the last video we wrote a custom filter to help us estimate how 2 00:00:05,280 --> 00:00:10,290 long a step would take to complete, based on the word count of its content. 3 00:00:10,290 --> 00:00:13,380 But we got stuck on how to test this filter, 4 00:00:13,380 --> 00:00:16,510 since we don't currently have a variable we can try it out on. 5 00:00:17,840 --> 00:00:22,190 But what if we could somehow use the built in word count filter? 6 00:00:22,190 --> 00:00:23,500 We're in luck, we can. 7 00:00:24,610 --> 00:00:29,020 We can use more than one filter on a variable, this is called chaining. 8 00:00:30,480 --> 00:00:36,240 So in this example, let's say we have a word in all caps, like HOORAY! 9 00:00:36,240 --> 00:00:38,980 We can apply the lower filter to it, 10 00:00:38,980 --> 00:00:42,840 which will display the word as hooray in all lower case. 11 00:00:42,840 --> 00:00:47,910 We can then apply the capfirst filter, which capitalizes the first letter in 12 00:00:47,910 --> 00:00:53,070 a string and the output will be Hooray with the first letter capitalized and 13 00:00:53,070 --> 00:00:54,379 the rest lower case. 14 00:00:55,580 --> 00:00:59,490 The filters are applied to the template variable in order. 15 00:00:59,490 --> 00:01:02,020 So, if we'd done cap first and 16 00:01:02,020 --> 00:01:06,490 then lower, the resulting output would be an all lowercase hooray. 17 00:01:07,520 --> 00:01:11,890 Each filter is applied to the results of the one before it. 18 00:01:11,890 --> 00:01:15,970 So let's see how we can make this work for us to try out our filter. 19 00:01:17,350 --> 00:01:20,720 Let's go back to step_detail.html and 20 00:01:20,720 --> 00:01:24,540 make sure we've loaded our custom filters into this template. 21 00:01:24,540 --> 00:01:26,390 So load course_extras. 22 00:01:27,820 --> 00:01:31,610 Good, now remember that filters are applied to the variable or 23 00:01:31,610 --> 00:01:33,960 expression that they're attached to. 24 00:01:33,960 --> 00:01:40,510 So right now, the expression con|wordcount, actually produces a number. 25 00:01:40,510 --> 00:01:43,390 The number of words in the variable content. 26 00:01:43,390 --> 00:01:47,610 That whole expression equals the number we need, the word count. 27 00:01:47,610 --> 00:01:51,960 So let's see if we can get our filter to work with the word count filter. 28 00:01:51,960 --> 00:01:55,020 We can add another sentence to our paragraph here, 29 00:01:55,020 --> 00:01:59,230 to test our theory about being able to chain these filters. 30 00:01:59,230 --> 00:02:02,040 So estimated time to complete. 31 00:02:03,330 --> 00:02:09,938 And then we can do con|wordcount|time_estimate, 32 00:02:09,938 --> 00:02:12,240 and then minutes. 33 00:02:13,750 --> 00:02:18,580 Now let's start our server by changing directories into learning_site and 34 00:02:18,580 --> 00:02:25,110 then running that familiar command python manage.py runserver, on our port. 35 00:02:27,730 --> 00:02:32,100 And now we can go into Python Basics, and 36 00:02:32,100 --> 00:02:35,510 using the shell and we can see that it works. 37 00:02:35,510 --> 00:02:39,895 The content of this step is 26 words, and our new 38 00:02:39,895 --> 00:02:44,895 custom filter estimates that it will take one minute to complete this step. 39 00:02:44,895 --> 00:02:49,015 But remembering what we said about chaining, let's use the pluralized filter 40 00:02:49,015 --> 00:02:54,175 on minutes for those courses, like this one, that might only take one minute. 41 00:02:54,175 --> 00:02:57,035 There probably aren't very many of those in the real world, but 42 00:02:57,035 --> 00:02:58,640 it's a good practice. 43 00:02:58,640 --> 00:02:59,800 How do you think we would do that? 44 00:03:00,970 --> 00:03:06,835 If the expression con|wordcount gives us a number for the word count, and 45 00:03:06,835 --> 00:03:12,400 adding |time_estimate gives us a number of minutes, we should be able to add 46 00:03:12,400 --> 00:03:17,960 pluralize to that expression to make minutes plural if we need it to. 47 00:03:17,960 --> 00:03:21,400 Depending on the number of minutes we estimate the step will take. 48 00:03:21,400 --> 00:03:23,060 So let's try that out. 49 00:03:23,060 --> 00:03:28,160 We can just copy this whole expression, save ourselves some typing. 50 00:03:28,160 --> 00:03:30,960 And remember when you're using the pluralize filter, 51 00:03:30,960 --> 00:03:34,590 you take off the s that makes the word plural. 52 00:03:34,590 --> 00:03:39,100 And just immediately after that word, you include your variable. 53 00:03:39,100 --> 00:03:43,315 So here after time_estimate, we add another pipe and 54 00:03:43,315 --> 00:03:46,910 we attache the pluralize filter. 55 00:03:46,910 --> 00:03:47,990 Let's refresh our page. 56 00:03:49,790 --> 00:03:51,280 And we can see it worked. 57 00:03:51,280 --> 00:03:54,330 Estimated time to complete, one minute. 58 00:03:54,330 --> 00:03:58,620 Theoretically, we could keep chaining as many filters as we wanted to, 59 00:03:58,620 --> 00:04:02,280 to one particular variable to get the result we needed. 60 00:04:02,280 --> 00:04:05,120 And this makes our data really dynamic and flexible. 61 00:04:06,600 --> 00:04:10,690 As you can see, chaining filters is really useful. 62 00:04:10,690 --> 00:04:14,310 we even mixed and matched between the built in filters and 63 00:04:14,310 --> 00:04:18,110 our own custom filters, and it worked just like we thought it would. 64 00:04:19,180 --> 00:04:23,470 For our last trick let's see what we can do with writing a filter 65 00:04:23,470 --> 00:04:26,210 that will convert mark down text to html.