PHP Laravel Tutorial for Beginners 5 - Laravel Blade Templating


 hey what's going on guys welcome back to our blog and welcome to this laravel for beginners tutorial series so actually this is the part 5 of this tutorial series and in this blog we're going to talk about blade templates and of course a sort of recap from the previous blog we work with the controllers and we set up controllers by using the artisan and as I've said in this blog we're going to talk about blade templates so let's go ahead and create a new folder inside our resources folder and under views so we're going to create here a layout directory so new folder and then we're just going to name it layouts because as you can see if we try to open up our pages the about the index and the services you can see we have repeating HTML for our services page our index page and our about page so we'll try to minimize that by using our layout files so under layouts this is under view so under layout we will create a new file called up that laid that PHP and then we're going to substitute or we're going to add some keywords here that will enable us to minimize this repeating HTML and inside here we're just going to copy the HTML from our about V dot PHP and paste it here in our app that laid dot PHP and in here we'll just say add symbol and then yield and and then keyword content so save this and inside our index dot PHP so instead of using all this HTML we'll just delete this and we'll just leave this h1 and paragraph tag and at the top we'll just say extends and then open in close single quote and then layouts that up so the reason behind we we put layout set up because this particularly out belongs or under layouts folder and the name of the layout is app so that's why we extend layout that up here and of course let's just delete this also the body and the HTML tag and in here was just gonna put a section and it should be the content right there and in here let's we'll just end the section okay there you go let us save this and copy all this code or maybe here and then go tour about that page P so instead of using all this HTML we'll just replace that and put the end section there same thing with our services so remove all that HTML and type in the end section here so let's go ahead and try to preview if all of our code are working so here we are at our index let's just refresh that and as you can see our index dot PHP file is still working with all the h1 and paragraph tag there and if we try to go to about it should work as well and of course the services okay so this is the index right so maybe we'll just change the stacks and try to see if it's working or if it's really working or not so let's go back to our code editor and index that blade that PHP so welcome to laravel for beginners and we'll just delete that save our file and let's go back to our browser mmm refresh there you go as you can see the h1 tag only contains now D welcome to laravel for beginners tax so in that manner we won't have to repeat all the ugly HTML that we had before and now let's try if we want to pass some values or parameters to our blade templates so let's go to our pages controller in the app HTTP and then under controllers so pages controllers and let's try to pass some values or parameters to our layout by just using this mechanism here so let's just say title and then welcome to programming knowledge something like this programming knowledge there you go and actually there's two ways on how to do this but I will show you both ways on passing values to our template so the first way is this so let's just pass parameters so view and then compact compact there and pass in the title as the parameter and let's go to our index so instead of using this text here let's type in curly braces and then tighten there we go and let's try to save this and go back to our browser so this is the original text welcome to larval for beginners and let's refresh our page so I think we have an error use of undefined constant title assume so I think we forgot to type in the dollar sign or money sign so instead of only the title we'll type in the dollar sign for our placeholder and then save that and refresh there you go as you can see welcome to programming knowledge so let's just just to make sure let's just change something here in our text so instead of the title let's just say welcome to programming knowledge and then let's try to add a mark there or at the exclamation mark and refresh our page and there you can see welcome to programming knowledge with the exclamation mark another way of passing parameters to our layout is by using the keyword width so instead of using this let's just comment this out and paste it here and also change this instead of compact let's just say width and of course the title keyword and the actual variable title let's just go ahead and do the same thing for our about pages so in here we can pass in the about us tax and this should be with and of course title with the actual variable title here and also in this particular method we can pass in multiple data by using array and instead of using the title variable so let's just say data and then we will define an array here array and inside this array let's just say we have a title here and then services okay and of course we need to pass this to our services page or services layout with the keyword with let's just not forget the greater than sign in of course you can use the theta volleyball and let's just go to our pages be about so instead of using the about here let's just go ahead and type in the title and also for our services here we can just say title there you go so about and our services and including our index that made that made that PHP and let's try to refresh our page and let's go to our about page as you can see we have here that dated about page and also the services with I think we have misspelled a bit so let's try to change that in our code editor so services I think it's in our pages controller and of course by deleting the D because we have misspelled there and refresh and there you have it services this is the service page so I think that's it for this tutorial and in the next blog we're going to discuss compiling assets and maybe models and database migrations so see you in the next blog thanks 

Post a Comment

Previous Post Next Post

Recent in Technology News