Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,440 --> 00:00:04,360
Before we wrap up this module, I want to implement one more thing
2
00:00:04,360 --> 00:00:09,410
here in the Home template. At the moment we have two posts in the
3
00:00:09,410 --> 00:00:12,320
database, but what if we had none?
4
00:00:12,600 --> 00:00:16,400
Let's test this scenario. Inside of the phpMyAdmin,
5
00:00:16,400 --> 00:00:20,150
I will remove both of the existing posts from the database.
6
00:00:20,540 --> 00:00:24,350
You can also do that in the command line or in whichever way you
7
00:00:24,350 --> 00:00:27,460
prefer, as long as the posts table is empty.
8
00:00:28,540 --> 00:00:32,380
If I refresh the Home page now, you can see that all we get is
9
00:00:32,380 --> 00:00:35,460
a blank page, which is kind of expected,
10
00:00:35,840 --> 00:00:39,260
but again, this is not really good user experience.
11
00:00:39,640 --> 00:00:44,060
We could of course add an if directive first to check if the post
12
00:00:44,060 --> 00:00:48,890
collection is empty before rendering this foreach loop, and there is
13
00:00:48,890 --> 00:00:51,970
nothing wrong with that approach. However,
14
00:00:51,980 --> 00:00:55,960
there is a better built‑in way to do this. Instead of using
15
00:00:55,960 --> 00:00:59,550
foreach, let's use the forelse directive.
16
00:00:59,750 --> 00:01:03,870
The functionality of these two directives is basically the same,
17
00:01:03,880 --> 00:01:06,960
but we get some additional features with forelse.
18
00:01:07,240 --> 00:01:11,780
We can add this empty directive inside of the forelse block
19
00:01:11,790 --> 00:01:15,050
to define what would happen if the variable that we are
20
00:01:15,050 --> 00:01:17,260
trying to loop over is empty.
21
00:01:17,540 --> 00:01:21,130
If that's the case, I will render a simple message.
22
00:01:21,370 --> 00:01:27,610
This empty directive works kind of like an else statement. And if I refresh the
23
00:01:27,610 --> 00:01:32,700
Home page now, you can see that instead of a blank page, we now get this useful
24
00:01:32,700 --> 00:01:35,680
message informing us that everything is fine,
25
00:01:35,680 --> 00:01:38,760
but there are no posts in the database yet.
26
00:01:39,140 --> 00:01:42,510
Maybe this doesn't seem like something important, but I think
27
00:01:42,510 --> 00:01:45,960
that you will often have the need to show something else in case
28
00:01:45,960 --> 00:01:48,120
that there is no data to display.
29
00:01:48,340 --> 00:01:52,160
If you have any questions, don't hesitate to post them in the discussion.2600
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.