Playing Next Lesson In
seconds

Transcript

  1. Now if you were paying attention, you probably noticed that our movies page looks different than our home page. Let's go ahead and compare. So here we have a monospace font and a dark grey background

  2. and our text is white. If we head back to our home page, our background is white, our text is black, and it's quite large. The difference here is how we're returning back the information.

  3. So if we right-click on our browser, jump down to Inspect, and head into the Network tab, this will allow us to inspect the network requests that our browser sends to our server. So let's go ahead and give the page a refresh

  4. to start capturing that, and there we go. We see a number of different things show up. The one in particular that we care about, though, is where the initiator is the document, because this is the request

  5. to our home page, and it's what's sending back what we see within the browser at this point in time. So let's go ahead and click on that, and let's head into the response. Cool, so what we see in the response

  6. matches exactly what we see within the browser, which is what you would expect, because this is exactly what the browser is using to show us our page. But if we click on Raw, this is where we start to see the HTML markup

  7. that makes up the page. And if you recall back to our earlier lesson whenever we were actually inspecting our page, let's dive into our resources, Views, Pages, Home.Edge. The HTML markup that we see in our response

  8. matches what we have defined within this page, including the It Works text. The only thing that's different is this fight declaration right here, which we'll get into at a later point in time.

  9. So the underlying reason why we see something different here as compared to our Movies page, so I'm going to go back to our Movies page now, give it a refresh so that we see the underlying request,

  10. let's click on the request for our document, and now we see the response. We don't have a Raw option at all for this response, and it's just sending back one single line that says "My Awesome Movie."

  11. There is no HTML markup associated with it. And that's because we're just sending back a plain text response for this particular request. Within our request table here, that's even noted within the Type column.

  12. You can see this one here is just plain, whereas if we go back to our Home page, that type is HTML. So how do we make our Movies page an HTML response?

  13. Well, let's go ahead and hide our browser back away, jump back into our Route definitions, and the primary difference between these two Route definitions is that this one is using a render function to render out a specific page,

  14. whereas with our Movies route, all that we're doing is returning back a string. So by default, this will return back just a plain text response, and instead, let's go ahead and get rid of our string,

  15. reach for our CTX, and if you'll recall back earlier, we have a View property within here. And that View property allows us to work with the Edge template engine. So let's go ahead and try to dive into

  16. the View. We can do . there, and look at that, we have a render method that looks relatively similar to what we are calling for our Home page. So let's go ahead and give that a try. So let's do Render.

  17. Okay, and now it's asking for a template path. So the template path is going to be the path to the file that we want to render for this page from within the Views directory. By default,

  18. Edge already knows to look within Resources and Views for our pages, so all that we need to do is to find the path from that point onward, which would be Pages, and then whatever page we make. So let's go ahead

  19. and create a new page by right-clicking on Pages, New File, and let's call this Movies.Edge. Let's jump into our Home page for right now, select everything by hitting CMD/CTRL+A, CMD/CTRL+C

  20. to copy it, jump back into our Movies page, and CMD/CTRL+V to paste it in. Now let's change our It Works text to "My Awesome Movie." We can give this a save,

  21. jump back into our routes, and now we have a template path to provide into our render function. So let's provide Pages, and you'll even see an autocomplete pop up here.

  22. This is thanks to the AdonisJS extension that we installed earlier, and we can populate this with Pages/Movies. Do note that you don't need to provide in the file extension of .edge,

  23. EdgeJS will imply that. So let's give this a save, our server will automatically pick up that change. We can dive back into our browser, let's give our page a refresh, and look at that, now it looks

  24. the exact same as our Home page, except the text says "My Awesome Movie." We can verify that we're now sending back an HTML response via the type right down here in our Network Inspector,

  25. we can even give this a click, and now we have a raw option where we see the underlying HTML. Awesome, so now we have an HTML page, but just having static HTML pages isn't too useful

  26. for a server that can serve up dynamic stuff. So how do we pass in dynamic data into this page and render it out? Well, let's go ahead and hide our browser back away,

  27. and let's see first if our HTTP context has any additional options for us. So let's do "ctx." We know that "view" is primarily for EdgeJS, so let's try searching within there.

  28. So we'll do "." again, we see "render raw," "render raw sync," "render sync," those are all rendering stuff, but then lastly we have "share." That sounds like it might be useful, so let's give it a try.

  29. This is a function, so we can call it, and it's going to ask for data of a type "record string any." This "record string any" is essentially asking for an object

  30. with a key-value pair. So we can provide an object with a key, let's call our key "movie," and let's call the value "my awesome movie." We can jump back into our movies page

  31. by following this underlying text right here within our render function, and hitting "cmd" or "ctrl" click on it, that will jump us straight into our page. And the way that we provide dynamic

  32. data into our Edge files is by entering double curly braces. And now we have access to stateful information provided into this view page. Within our share method, we called

  33. our key "movie," so let's go ahead and try typing in "movie" to see if that works. Let's give this a save, jump back into our browser, we can give our page a refresh,

  34. and look at that, we still see "my awesome movie." Let's hide our browser once more and give ourselves a sanity check just to make sure that it'll work. So let's jump back into our routes, and let's do "another awesome movie."

  35. Give that a save, jump back into our browser, give our page a refresh, and voila, there's another awesome movie. Cool, so now we have dynamic data coming into our page that we're now

  36. rendering out, but we're not quite done yet. There's actually a more streamlined way that we can do this exact same thing. So let's hide our browser back away. If we take a look at the options that our render function has,

  37. you'll see that the second argument is for state information. Well, we're using those double curly braces to gain access to stateful information provided into our EdgeJS files. So what if we tried

  38. providing that state directly via this second argument? You'll notice that the type is "record string any" or "undefined." This means that we can either provide in an object with a key value

  39. pair, or we could provide in "undefined" or "nothing," since this also is completely optional. At this point, we're providing a "nothing," but if we highlight our movie data,

  40. cut it out with Command or Control X, get rid of our share call altogether, let's provide in a comma, and let's paste this back in as the stateful information to our render call.

  41. Give this a save, let's jump back into our browser at this point. We're still seeing another awesome movie, so we can imply that it works. Let's hide our browser back away, give it another sanity check,

  42. switch this back to "my," give this a save, jump back into our browser once more, give the page a refresh, and voila! There is my awesome movie again. So at this point, we know that we have

  43. two different options to provide stateful information into our EdgeJS files that we can render out as HTML.

Rendering a View for a Route

@tomgobich
Published by
@tomgobich
In This Lesson

We'll learn how we can use the EdgeJS Template Engine to render HTML views and send them back as the response for our routes. We'll also see how we can pass dynamic data into our views from our route handler.

Join the Discussion 8 comments

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

    It would be cool to get some tutorials on using other output options, like pushing to a SPA or using a different templating engine.

    1
    1. Responding to frp
      @tomgobich

      Agreed, and thank you for the suggestion! It'd be good to cover those things, and we will most certainly get to them! It won't be within this series though, as we want this series to be a good entry point for everyone coming to AdonisJS 6, and using EdgeJS is the most inclusive way to do that as it's a part of the framework.

      We'll plan and add series for these into our schedule!

      0
      1. Responding to tomgobich
        @frp

        Thanks Tom. I imagine you also will be updating a lot of your v5 lessons to v6. I really like your style. I find you way easier to understand than most tutorialists

        1
        1. Responding to frp
          @tomgobich

          Yeah, I've got quite a bit planned including additional focus on project-based series that'll cover updated topics we covered in v5! I'll be putting 100% of my focus on this series until we wrap it up, then we'll move onward from there 😄

          Also, thank you very much that's very kind!! 😊

          0
          1. Responding to tomgobich
            @frp

            Hi Tom, can you tell me why when I inspect the request object the parsed url host, protocol, etc are always null? I'm not sure the difference between host and hostname (if they were not null I could figure it out haha), but I want to be able to key off of that value in my code. I could grab the host header and parse that, but I'd rather just use the parsedUrl property for everything.

            
              "parsedUrl": {
                "protocol": null,
                "slashes": null,
                "auth": null,
                "host": null,
                "port": null,
                "hostname": null,
                "hash": null,
                "search": null,
                "query": null,
                "pathname": '/welcome',
                "path": '/welcome',
                "href": '/welcome'
            1
            1. Responding to frp
              @tomgobich

              It looks like AdonisJS is using the request.url to parse these details out using the parse method from node:url and it looks like the request.url only contains the path for the request. So, it's being built without those details included and since the parsedUrl object is just an instance of URL that'd be why they're still included despite being null.

              You can get a complete parse of the URL by doing:

              router.get('/my-endpoint', (ctx) => {
                // passing true to completeUrl will include the query string
                const completeUrl = new URL(ctx.request.completeUrl(true))
                return completeUrl
              })
              Copied!

              Host and hostname will mostly be the same. The difference is, if the URL contains a port, host will include it and hostname will not.

              REQUEST: https://test.com:3333/endpoint
              HOST: test.com:3333
              HOSTNAME: test.com
              0
              1. Responding to tomgobich
                @frp

                Thanks! I followed the link to the source code and it looks like all I have to do to get what I want is request.hostname(), and that method just pulls the info out of the headers anyway.

                0
                1. Responding to frp
                  @tomgobich

                  Anytime! Yep, that should do it! 😊

                  0