Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Chaining Filters4:27 with Lacey Williams Henschel
Learn how to chaining filters works and how to do it on your site.
In the last video we wrote a custom filter to help us estimate how 0:00 long a step would take to complete, based on the word count of its content. 0:05 But we got stuck on how to test this filter, 0:10 since we don't currently have a variable we can try it out on. 0:13 But what if we could somehow use the built in word count filter? 0:17 We're in luck, we can. 0:22 We can use more than one filter on a variable, this is called chaining. 0:24 So in this example, let's say we have a word in all caps, like HOORAY! 0:30 We can apply the lower filter to it, 0:36 which will display the word as hooray in all lower case. 0:38 We can then apply the capfirst filter, which capitalizes the first letter in 0:42 a string and the output will be Hooray with the first letter capitalized and 0:47 the rest lower case. 0:53 The filters are applied to the template variable in order. 0:55 So, if we'd done cap first and 0:59 then lower, the resulting output would be an all lowercase hooray. 1:02 Each filter is applied to the results of the one before it. 1:07 So let's see how we can make this work for us to try out our filter. 1:11 Let's go back to step_detail.html and 1:17 make sure we've loaded our custom filters into this template. 1:20 So load course_extras. 1:24 Good, now remember that filters are applied to the variable or 1:27 expression that they're attached to. 1:31 So right now, the expression con|wordcount, actually produces a number. 1:33 The number of words in the variable content. 1:40 That whole expression equals the number we need, the word count. 1:43 So let's see if we can get our filter to work with the word count filter. 1:47 We can add another sentence to our paragraph here, 1:51 to test our theory about being able to chain these filters. 1:55 So estimated time to complete. 1:59 And then we can do con|wordcount|time_estimate, 2:03 and then minutes. 2:09 Now let's start our server by changing directories into learning_site and 2:13 then running that familiar command python manage.py runserver, on our port. 2:18 And now we can go into Python Basics, and 2:27 using the shell and we can see that it works. 2:32 The content of this step is 26 words, and our new 2:35 custom filter estimates that it will take one minute to complete this step. 2:39 But remembering what we said about chaining, let's use the pluralized filter 2:44 on minutes for those courses, like this one, that might only take one minute. 2:49 There probably aren't very many of those in the real world, but 2:54 it's a good practice. 2:57 How do you think we would do that? 2:58 If the expression con|wordcount gives us a number for the word count, and 3:00 adding |time_estimate gives us a number of minutes, we should be able to add 3:06 pluralize to that expression to make minutes plural if we need it to. 3:12 Depending on the number of minutes we estimate the step will take. 3:17 So let's try that out. 3:21 We can just copy this whole expression, save ourselves some typing. 3:23 And remember when you're using the pluralize filter, 3:28 you take off the s that makes the word plural. 3:30 And just immediately after that word, you include your variable. 3:34 So here after time_estimate, we add another pipe and 3:39 we attache the pluralize filter. 3:43 Let's refresh our page. 3:46 And we can see it worked. 3:49 Estimated time to complete, one minute. 3:51 Theoretically, we could keep chaining as many filters as we wanted to, 3:54 to one particular variable to get the result we needed. 3:58 And this makes our data really dynamic and flexible. 4:02 As you can see, chaining filters is really useful. 4:06 we even mixed and matched between the built in filters and 4:10 our own custom filters, and it worked just like we thought it would. 4:14 For our last trick let's see what we can do with writing a filter 4:19 that will convert mark down text to html. 4:23
You need to sign up for Treehouse in order to download course files.Sign up