Playing Next Lesson In
seconds

Transcript

  1. set up and ready to go. So first let's move our welcome page into a separate directory called pages inside of our views directory.

  2. So let's go and create a new folder and let's call this pages and then we'll just want to move our welcome page directly into there. Next we'll need to move into our routes to make that adjustment.

  3. So render just changes to pages slash welcome. Cool. So we should be good there. Now we're going to create all of our pages inside of the pages directory just kind of keep them separated from everything else.

  4. Now for our layout we're going to do something a little bit different since we're focusing on components within this series. We're not going to use your typical layout to create our actual layout.

  5. Instead we're going to use a component to create our layout. So let's go ahead and get our components set up here. Within our project we're going to want our components to reside underneath the views

  6. directory inside of a new folder and we'll want this folder to be called components. We'll do a slash. Now we can place all of our layouts inside of a separate folder inside of components

  7. called layouts and then we'll just call this app dot edge. So what this did is created a components directory with a layouts directory inside of it and a file inside of it called app dot edge.

  8. Within our app dot edge let's copy all of the HTML markup from our welcome page and paste it directly in here.

  9. For the actual contents we're going to have a div with a class of max width for Excel MX auto and M y 8.

  10. Inside of here we're going to have kind of a header section so we'll do header class

  11. BG gray 100 P 6 negative MX 6 margin bottom 6 and rounded MD.

  12. Inside of our header we're going to have kind of a breadcrumb so if we're not on the home page we want to display a link back to the home page.

  13. So we're going to do at if title does not equal home and their if statement then we'll

  14. just play anchor back to that home page with a class of text extra small and call that

  15. home and then underneath here we're going to want to display our H1 for our actual header

  16. class text to Excel bold and we'll display our title there and then underneath this header

  17. section is where we're going to want to display the actual page contents. Now typically within a layout you would use a section for this however since we're inside

  18. of a component all we need to do is use the main slot for this to render out the main

  19. slot we just do three backticks a weight slots and then call the main function.

  20. The main slot is always going to be provided even if you don't provide any content into the component itself so we don't need to worry about this being undefined or null here and

  21. the three backticks as opposed to the two backticks allows us to plop raw HTML into the page which we'll need to do for the slot.

  22. Cool so we should have our layout all set up and ready to go let's dive back to our welcome page and give it a run so we can get rid of everything inside of here and we actually

  23. have a couple of options on how to actually go about rendering out our component so we

  24. can either use the component tag and go to our components slash layouts slash app give

  25. it our title of home and our component and then do slot content here so that should work

  26. just fine so we can go ahead and dive back into our browser looks like we have an error from whenever we move the page just give the page a refresh that should be fixed and there we go so we have our basic layout here we have our header section up here rendering

  27. out home and then we have our slot content here so our layout is working just fine as a component however with the way that we called this we use the component tag to render this

  28. out since we named this folder components inside of our views directory we're actually

  29. able to use the component directly as a tag and that's because Adonis JS renders those

  30. components inside of the views components directory globally as edge tags the example that they give in the documentation here is a button so they have a button at resources

  31. views components button dot edge looks like this and they're showing that you're able to render that out by just reaching directly for button off of the at character referencing

  32. it as a tag directly if you're using folders as we are with our layout you're able to reach directly inside of nested folders using dot to separate out between those folders and

  33. then reach inside of it directly for a file you have a couple of examples here so if you have form input dot edge you reach for it via form dot input tool tip would be tool

  34. capital T tip checkout form would be checkout capital F form dot input and for our case

  35. if we dive back into our file we can replace our component here with layout dot app and

  36. of course adding back in our home title and then end this out we just get rid of component from our tag and just have end there give that a save and everything should still be working a okay.

View Structure and Component-Based Layouts

In This Lesson

We'll get our view and component structures set up. Then, we'll take an abnormal approach and cover how to use components for your layouts.

Created by
@tomgobich
Published

Let’s take a brief moment to get our view structure prepared for the various pages and components we’ll be building in this series. Then, to keep with the component theme of this series, we’ll learn how we can utilize components for layouts instead of the traditional EdgeJS Layout approach.

Organizing Our Views

First, let’s move our welcome page into a separate directory called “pages” within our views directory.

mv resources/views/welcome.edge resources/views/pages/welcome.edge
Copied!

Next, let’s move into our start/routes.ts file to update the path for this page.

Route.get('/', async ({ view }) => {
  return view.render('welcome')
  return view.render('pages/welcome')
})
Copied!
  • start
  • routes.ts

Cool, so we should be good there. From now on when we create a page, we’ll be creating it within resources/view/pages to help keep our pages separated from everything else.

Using Components for Layouts

For our layout, we’re going to do something a little bit different. Since we’re focusing on components within this series, we’re not going to use a typical layout for our layout. Instead, we’ll be creating a component to serve as our layout.

First, let’s create a components directory inside the views directory.

mkdir resources/views/components
Copied!

Then, within the components directory, we’ll create a subdirectory called layouts with a file inside it called app.edge. This app.edge file will serve as our application’s layout.

mkdir resources/views/components/layouts
touch resources/views/components/layouts/app.edge
Copied!

Creating Our Layout Component

Let’s then copy all the markup within our welcome.edge page and paste it into our new app.edge layout file.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  @entryPointStyles('app')
</head>
<body>
  <h1 
    x-data="{ text: 'Hello world!' }" 
    x-text="text"
    class="text-3xl font-bold underline text-red-500">
  </h1>
</body>
</html>
Copied!
  • resources
  • views
  • components
  • layouts
  • app.edge

For the actual body content, let’s replace our test h1 with the below.

<div class="max-w-4xl mx-auto my-8">
  <header class="bg-gray-100 p-6 -mx-6 mb-6 rounded-md">
    @if (title != 'Home')
      <a href="/" class="text-xs">Home</a>
    @endif
    <h1 class="text-2xl font-bold">{{ title }}</h1>
  </header>

  <main>
    {{{ await $slots.main() }}}
  </main>
</div>
Copied!
  • resources
  • views
  • components
  • layouts
  • app.edge

First, we’re accepting a single prop to this component called title.

Within our header, we have a simple check that will display a breadcrumb back to the home page if the title does not equal “Home”.

Then, we’ll display the provided title prop within an h1.

Lastly, we’ll render our main slot content inside a main element.

The main slot is always going to be provided even if you don’t provide any content into the component’s slot. So, we don’t need to worry about this being undefined or null here. The three curly braces as opposed to the two curly braces allows us to plop raw HTML into the page, which is exactly what we’ll need to do for slots.

Using Our Layout Component

Our layout is now set up and ready to go, let’s now dive back into our welcome.edge page and get rid of everything inside of here so we’re left with an empty file.

To actually make use of our layout component we have a couple of options. First, we could use the component tag.

@component('components/layouts/app', { title: 'Home' })

  <h1>Page contents here</h1>

@endcomponent
Copied!
  • resources
  • views
  • welcome.edge

This, though verbose, will work just fine. However, we have a shorthand readily available to use since we strategically placed our components directory directly inside our views directory. AdonisJS will automatically register EdgeJS files placed within resources/views/components as direct tags, meaning we can do the below.

@layouts.app({ title: 'Home' })

  <h1>Page contents here</h1>

@end
Copied!
  • resources
  • views
  • welcome.edge

We can now reach directly for our layouts directory as a tag, via @layouts, then we can reach inside this folder using a dot, and finally specify the file name of the component we want to use.

Here are a couple more examples of component-to-tag name conversions.

@!component('components/pagination')
@!pagination()

@!component('components/menu_bar')
@!menuBar()

@!component('components/tab/data')
@!tab.data()

@!component('components/text/animation/drop_in')
@!text.animation.dropIn()
Copied!

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!