Intro to Phoenix: About Us Page (Part 2)

Phoenix Elixir

English (Auto-generated)

Hello Friends. Let's continue with the about us page. If you remember from the last cast, what we did is we designed the about us page to take in a variable that is passed as an http program which is done by putting the question mark and then name equals and then you put the programs like this, you can pass in other problems as well, such as, you know, city equals Vancouver and so on. So these are standard http program that you could pass with your your l uh one problem that we face that, you know, if we don't have a program even though we did design our which controller to have a function uh that matches when the name parameter is not passed. So in this case here, if programs has a name key in it, it will actually match to this function. Hand, if not, then it will default to this one here which accepts no uh parameters in the brands. So that's why it's gonna go here, which worked. But then it's saying that key name is not found in this map. Regular. So as you might be guessing here, um when you pass in the keyword list here, it passes it seems like it passes it as a map to the um if you fall to the template through the view file and here we could actually use it. So in this particular case here we're using it as this variable name right here. Okay. But if you don't have it, I mean you may be inclined to do something like well if that name Yeah. And then you can put and right here, but that doesn't solve the problem because it's not like it's coming as as nil, it's actually not being assigned altogether. So the solution for this particular case is to do a science something like this as and then if I refresh the page now it works, if I pass it with a name then that also gets displayed as well. Okay. Um so as you can tell here with Higgs files, we can actually embed bits and pieces of elixir code to make our template more dynamic. You're probably used to this in different template ng systems, depending on the language you came from. For instance, javascript have e J s as one of the available options and ruby has er b this is somewhat similar I would say of course there's some differences due to the nature of elixir. Um but more or less you will see here this this code may look very familiar to you if you've done any web development in the past so um I'm not here that notice I'm using if um which is totally okay. Um some elixir developers like to avoid using a lot of f in favor of things like pattern matching or case I would say in view files I noticed more often than not they're actually commonly used as it makes the code more readable. Okay, so that's why I guess you'll you'll see this type of code very commonly in uh in electoral. One more thing I'd like to show you here, which is something that we haven't really dug into, which is the view file. So the view file here seems to just be a module that have a use for this view that's coming from dev notes, which really inherits a lot of functions from uh, phoenix. So that's why again, just by have having this here as a default, it comes built in with a lot of functions. So for example, when we do things like we did he recall in in the root layout here, a lot of these functions are built in with phoenix and basically they get imported from it. Now that said here, these view files are really a good way to organize your functions in a way when you want to use it. So let's say here, I want to say something like I'll define a function called name, display takes in a name as an argument and then let's say here, I'll just do string that capitalizes name. Okay, it's a very simple function. It's simple enough that I could probably just have it as a single line function. So when you define a function like this in here, you could simply just go ahead and use this function in your view of files. You could do name display and then pass it the name. Bridget. So, um, this way here you could keep, for example, all the functions that are commonly needed for uh templates that are used for the page controller right into this space view. And then you could you could reuse them, you could use them in in any of the templates for the view. So I could use it in the index here as well if I really wanted to. So here let's try to pass in like a lower case them. It's still spaces capital case because we've called the function awesome. So um that should be it right now for this lesson, so we've learned how to create new pages, we've learned how to start our project um although not specifically related to phoenix per se, it's always a good practice to add uh to make sure you commit regularly to get commit dash. Um I could do something, build the about us page, even though we haven't really finished it or just have a good commit message right there. Thank you for tuning in and I'll see you in the next guy.
37 Views 0 Likes 0 Comments

Let's finish off our About Us page!

Leave a comment (supports markdown format)