Build an "SPA" with HTMX #2 - Showing Initial Data

Net Ninja · Intermediate ·🔧 Backend Engineering ·1y ago
Skills: HTML & CSS60%

Key Takeaways

This video demonstrates how to build an SPA-style application using HTMX attributes, focusing on injecting initial data into the index template and displaying it in a grid of cards.

Full Transcript

okay then so in this lesson we're going to inject some initial data into the index template so we can show some actual content for that data and I want to let you know before we start this lesson that the actual htx content in this video is pretty much none we might mention it a little bit and how it's going to work in the future but for now we're going to focus on just making an index view or fleshing out the index view which lists out a bunch of data and that doesn't really need any htx but then in the next lesson we'll be adding some htx to The View anyway the data we're going to be using is just some dummy data that we can output in a grid of cards on the page and that data lives inside this data folder in the articles. JS file and you can see this just includes an export and a rate of three article objects each with an ID property a title and a body so then we want to make this articles array available within the index view so that we can see it and to make data available in a view POG we should pass it into the view as a property on the object when we say which view we want to render so in the routes file we can already see that we pass a title property through and this is how we can pass values into views right as properties on an object as the second argument to this render method and those properties are then accessible in whatever view we pass them into in this case it's the index View and also any partial views nested within the index or any views the index extends like the layout view so anyway we already pass a title in and that actually gets used in the layout view but now we also want to pass through an articles property so let's make that property first of all and then we also need to import the Articles data from the Articles file at the top of this rout file so that we can pass that value into the view so let's do that by saying import articles and it comes from. SL to come out of this folder then into Data and then SL articles. JS and once that's done we can then pass those articles as a value to the Articles property in this object meaning that now we should be able to use this articles value inside the index view so let's head to that index View and use those articles and in this articles view I want to make a new div beneath the H2 with the class of article list to do that we can just say div. article hyen list and actually we don't even need the div part we can leave that off because pug uses divs as the default tag when one isn't specified so if we just say do article hyphen list then this will render a div with a class of article hyen list now actually I want to make a new partial View for the list of articles for two reasons one because it makes that partial view reusable if we want to use it elsewhere later which I think we will and two because it keeps this view this index view cleaner so let's first make a new folder inside the views folder called partials for any partial views and inside that folder we'll make a new file called list. pug since this is going to be a list of articles in here you can call it what you want though all right then so before we flesh this view out I want to make sure that we Nest or include this view inside the index one so let's go back to that view and beneath the article list div tab wants to say we're nesting something in this div and then say include followed by a path to the view we want to include so partials to go into the partials folder then slash list right then so now back in the list view we still get access to the article's property because this view is nested within the index view which we passed the Articles into so what do we want to do with the Articles data inside this file then well remember the Articles data is an array and we want to Output a bit of template for each item in the array so we want to somehow Loop through these output a bit of template for each one the way we do that in pug is by saying each article in articles so articles is the data we have and we refer to each one as an article you can call it XY Z if you wanted to but I'm calling it article and then again we have to indent and we can output the template here and what pug will do is cycle through the Articles and for each one output this bit of template so let's say div right here and we'll give each one a class of article and and then below that div remember we don't actually need to write the div we can just say do article and it will still be a div but then within that div we want to Output three things a title of the article then some kind of body snippet so a short section of the body and then also a button at the bottom that says view more details or something like that so let's do an H3 first and then to output something Dynamic we use a pound sign or hash and then curly braces and then we can access the variable so article in in this case this thing right here and then dots and then whatever property we want now it's the name property if we take a look at this we have name and body all right so we output the name below that we'll do a P tag for the article body so again hash and then curly braces article body and I don't want to Output the entire body because that would be a huge paragraph instead we'll slice a small section of that so we'll say do slice and then we'll go from position zero to 100 characters so we're just getting the first 100 characters here and outputting those and then after those 100 characters we'll do dot dot dot to say there's more all right and then finally a. BTN um let's do that correctly BTN and we'll say view details for the text of that link all right and by the way we have already created styles for these classes right here so everything's going to look okay in the browser Okay cool so that is the list view sorted all right cool so now we can see all those articles on on the homepage and everything seems to be working so in the next lesson we'll set up these buttons to navigate to a details view for each article using HDMX to basically swap out content on the screen and update the URL in the address bar

Original Description

In this HTMX series, you'll learn how to build an SPA-style application, using just a few HTMX attributes. 🔥🥷🏼Get access to premium courses on Net Ninja Pro: https://netninja.dev/ 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/spa-with-htmx 📂🥷🏼 Starter project on GitHub: https://github.com/iamshaunjp/spa-with-htmx/tree/starter-project 🧠🥷🏼 HTMX Crash Course: https://www.youtube.com/watch?v=Yr-ubS0H7z4&list=PL4cUxeGkcC9gnEsXRqdY4e_xNy9GK7aQR 🧠🥷🏼 Node.js & Express Crash Course: https://www.youtube.com/watch?v=zb3Qk8SG5Ms&list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU 🔗🥷🏼 HTMX docs: https://htmx.org/docs/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to inject initial data into an index template and display it in a grid of cards using HTMX and Pug. It covers topics such as routing, view properties, and looping through data.

Key Takeaways
  1. Create a new file for dummy data
  2. Import the data into the routes file
  3. Pass the data as a property to the view
  4. Create a new partial view for the list of articles
  5. Nest the partial view inside the index view
  6. Loop through the data and output a template for each item
💡 Using HTMX attributes and Pug templates can simplify the process of building an SPA-style application.

Related Reads

Up next
Beginners Guide to GPT4 API & ChatGPT 3.5 Turbo API Tutorial
Adrian Twarog
Watch →