02. Installing and x-data


OK. So let's go ahead and get Alpine pulled in in this part. And we'll output a really simple hello world example
just to get us started. So if you come over to the Alpine.js home page, you'll see that there's a script just here. It's fine if you want to pull that in.
What I recommend you do is come over to the installation section here and pull in the specific version that you need. What that means is if there are any changes in version 3, they're not going to immediately break your code.
So here you can see the example of pulling in 3.1.1. Let's go ahead and just pull in this for now, the home page, just to get us started. It's a little bit easier.
And let's come over to this index.html page, get a really basic document layout here, and just put this in the head of the document. That is Alpine installed.
If we go ahead and open this up here, literally nothing has changed. We just have Alpine pulled in on the page. So what we're going to do is talk about Alpine components.
We can register multiple Alpine components on a page. And these are just the small pieces of your application that use Alpine functionality. So for example, this could be a div, it could be a header,
could be an input, pretty much anything that you can put on your documents. So what we do to initialize Alpine is use xdata. Now, this doesn't have to have an object.
You can literally just use xdata on its own. What we now have is an Alpine component on the page, of course, with nothing in there at the moment. So that's the kind of fundamental initialization
of this component. Now, inside of xdata, what we have is an object. And this can contain properties. It can contain functions.
So you can pretty much put anything you like inside of here. For example, we could have just a piece of text, like hello. And we could assign that the value world. Now, what we could do inside of here is access that,
modify that, use that to render something out conditionally. And normally, you would do that with Boolean values. But in our case, we just want to output this string inside of here.
Now, normally, when we output things within frameworks, JavaScript frameworks, we would perhaps use curly braces here to output the value that we wanted.
We don't do that in Alpine. What we do is instead create out another element, really just depending on what kind of data you're working with.
And then we use the xtext directive in here. And we go ahead and reference the value that we want to see. What we'll see in here, if we just give this a refresh, is world.
We don't see that at the moment, just because we want to go ahead and prefix this with HTTPS. And there we go. Alpine is actually pulled in now.
And we see the value of that property inside of here. Now, anything inside of xtext, we'll speak about this later. And we'll dive into more complex examples. This could be an expression if you wanted it to be.
So for example, you could concatenate on something else into here. So for example, if we go and give that a refresh, we see that that's concatenated in.
So this can be an expression as well. You could even add two numbers inside of here if you wanted to and get the result. Doesn't really matter what you put in here.
But usually with xtext, we would use this to reference a value that we have stored inside of xdata to output a piece of information. Now, inside of here, like I said, you can have functions.
You can have just plain properties. You can even have getters, which we'll talk about later as well. But what we would normally think about doing is doing this all in line because it's an attribute
almost inside of this element. What instead we would usually do is just pull this down so it's a little bit neater to work with. So I could just pull this down like this.
Might look a little bit messy, but you get the benefit of having this like a normal object that you would split down on multiple lines. So if you had some function inside of here
that did something, it looks a lot cleaner to do it like this. For now, though, that is Alpine installed and a really basic example of outputting a piece of information.
19 episodes2 hrs 10 mins


Alpine.js is a refreshingly minimal JavaScript framework that gives you the reactive nature of Vue and React, but with much more simplicity. This course will get you up to speed on how to use it, with plenty of practical examples along the way.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.