Playing Next Lesson In
seconds

Transcript

  1. So while we're on the topic of components, let's go and talk about layouts. Layouts within EdgeJS,

  2. if you're coming from AdonisJS 5, have been removed. And they've been removed in favor of components because components have a lot more capabilities

  3. in terms of what they can do, and allows you to do a lot more with your layouts as a whole. So up till now, if we dive back into our text editor for our pages,

  4. we've been providing the markup for our entire HTML body as a whole for each page that we have. Here's our homepage, and here's our movie show page. What we can do is copy everything,

  5. including and up to our navigation, give that a copy. And let's go ahead and right click our components folder and click new file. And let's create a folder called layout. We can have multiple layouts,

  6. but we can define a base layout of just index.edge, which will allow us to use it very similarly to our button where we can just call at layout and use the default.

  7. Now we have a layout folder with an index file inside of it. And let's paste that markup that we had copied earlier. We'll end our div and end our body. And we already know about main slots.

  8. So we can provide our main slot in where the main body content would go. So that's gonna be slots.main, and don't forget to await it. And give this a save. Now we can jump into both of our pages.

  9. So our movie show page, we can get rid of everything up to our navigation, replace that with at layout, just like so. And we can get rid of the body and end HTML here as well.

  10. Cool, so that should be good for our show page. Let's do our homepage next. Everything up through our navigation, replace that with at layout, scroll down, get rid of the end body and HTML,

  11. and replace that with an end for our layout component. Cool, we can give that a save. Let's jump into our browser real quick and make sure everything's working A-okay still. Looks good on this page. Let's dive into a movie page.

  12. Looks good there too. Awesome, we'll hide that back away. So within traditional layouts, you would have sections, right, you would have maybe your main body section, maybe a meta section, maybe a footer section, so on and so forth.

  13. Each of those sections can be easily replaced with slots. We've already done so with our main body section by replacing it with the main slot. Let's go and do the same. Let's dive back into our layout component for metadata.

  14. So within our head here, we'll put this above our byte declaration, three curly braces, await slots.meta. We can put any meta information that we want to

  15. within our HTML head by specifying it into our meta slot. Now let's give this a save and we're gonna see an error, but let's dive into our browser to see what the error looks like.

  16. So we're gonna see slots.meta is not a function. Within our components, the only slot that will always be provided is the main slot. The others are nullable.

  17. So if we don't specify a meta slot here as per scene, we're gonna get an error because that's not gonna be a function. Not only that, it's not going to exist.

  18. The value will be undefined whenever we try to read it. So if we hide our browser back away, we'll want to wrap this particular function in an if check, since we want it to be optional.

  19. We don't wanna force ourselves to use it in order for our page to render out successfully. So we do @if slots, and we can just read for meta to see if it has a value.

  20. If it does, it will be a function and we'll be able to apply our slot. So now we can give that a save, jump back into our browser, and we should be good to refresh our page here and everything's back to working.

  21. Cool. Let's go ahead and add some metadata. So let's go down to our homepage. Oh, hold on a second. Looks like we have an extra div down here. Let's go ahead and get rid of that. There we go.

  22. Okay, so to specify a name slot that we wanna use, we'll do @slot, and we'll provide it in the name of the slot that we wanna provide data for. And our slot as well.

  23. And anything in between the start and end slot tags, we'll go into the name slot that we've provided. It's been a while since I've written this, so I might get the structure here wrong,

  24. but I think it's meta name description. And I think it's content equals our awesome movie list. And we can end that meta tag there.

  25. Let's give it a save and see whether or not it worked. Step back into our browser, let's right click, inspect, head into the head. And sure enough, there's our meta description right there. If you wanted to, you could do the same thing for your title.

  26. Alternatively, the title is also a good option for a prop. So if we dive back into our layout for the title here, let's go ahead and replace it, double curly braces.

  27. And we just provide title there if it's provided as a prop. Otherwise we'll provide, let's learn AdonisJS 6. So if we give this a save, before we take a look at it,

  28. let's dive down to our show page for our movie and let's provide a title here. So we'll provide title in here and this will just be our movie title. And then we'll also provide in a slot

  29. for our meta description and our slot. We'll do meta name description content equals and we'll apply our movie summary into there.

  30. I think we also have that extra div here as well. Yeah, it looks like it. So we can get rid of that there too. So let's go and dive back into our browser now and take a look at this. So first things first, we can see directly up in our title,

  31. the title for our homepages. Now let's learn AdonisJS 6. If we right click and inspect, take a look at the head, we see exactly that, the title and our description is still there. Let's go and dive into one of our movies.

  32. So let's dive into this one. We can see the title for this one is now Awesome Movie, The Trilogy. If we dive into the head, we see exactly that. And we see that the description is the summary for the movie.

  33. So everything there seems to be working A-OK, which is awesome. So we can hide that back away, hide our browser back away. And now anytime that we create any additional pages for our application, we no longer need to provide

  34. the entire bootstrapping of the HTML markup, but rather just reach directly for our layout. And not only will it provide that HTML bootstrapping,

  35. but it will also provide our navigation along with it. And later on, we can add in a footer or any other details that we want across all of our pages directly within our layout.

Extracting A Layout Component

@tomgobich
Published by
@tomgobich
In This Lesson

We'll learn how we can create EdgeJS layouts using components so that we don't have to redefine or HTML structure for every page in our application.

Join the Discussion 4 comments

Create a free account to join in on the discussion
  1. @redeemefy

    Hi Tom,

    Just wonder… the Edge docs encourages the use of components instead of layout. I can see the repo in the final version of the application and you are still using the layout() in the index.edge file.

    Is there anything I'm missing here?

    1
    1. Responding to redeemefy
      @tomgobich

      Hi Redeemefy!!

      Although we're calling it as layout(), it is created and defined as a component! When you place your components within resources/views/components AdonisJS will automatically register those components as EdgeJS tags, which is what allows us to refer to our layout as layout() rather than component('components/layout').

      Hope this helps!

      1
  2. @random123

    Hi Tom,
    I am new to adonisjs and template engines. I wonder how to write CSS without tailwind with my custom classes.I want to write my style and component in the same file. For example:

    /resources/components/button/index.edge

    <button class="my_class">…</button>
    <style>
    .my_class {
    background: red
    }
    </style>

    And style names won't be conflict with the same name in another edge file. Just like in Astro framework or React Native. I think they call it component base CSS.

    1
    1. Responding to random123
      @tomgobich

      Hi Random123!

      This isn't typically something Template Engines provide. Template Engines are really only concerned about generating markup and don't concern themselves with how that markup is used after it's generated.

      You can look at potential third-party solutions to add this, though. I've never used it, but one that may solve what you're after is css-scope-inline.

      1