Blade templates & Layouts | Laravel 10 Tutorial #7

Yelo Code
15 Jul 202314:27

Summary

TLDRIn this tutorial episode, the host guides viewers through setting up the HTML template for a project using the 'Lara Blade' framework. They demonstrate how to integrate the 'index.html' file, customize the layout with Bootstrap 5 and Swatch, and create a clean, reusable dashboard. The episode also covers creating additional pages like 'terms and conditions', implementing a shared layout to avoid code duplication, and utilizing Blade's 'yield' and 'section' directives for efficient templating. Finally, the host shows how to use configuration files to dynamically change the application name displayed on the site.

Takeaways

  • 😀 The video is a tutorial episode focused on creating a layout for a project using HTML and Laravel's Blade templating engine.
  • 🔧 The presenter introduces an HTML template from a GitHub repository and demonstrates how to integrate it into a 'dashboard.blade' file.
  • 🌐 The template utilizes Bootstrap 5 and a 'bootswatch' for a distinct visual style, emphasizing simplicity and ease of use.
  • 📝 The presenter removes unused sections, such as the profile page, to streamline the layout for the current episode's focus.
  • 🎨 A 'terms and conditions' page is created as an example of how to add new pages to the application without duplicating code.
  • 📑 The video explains how to create a reusable layout to avoid code duplication across multiple pages in a Laravel application.
  • 📐 The 'yield' directive in Blade is introduced as a way to define a section of the layout that can be filled with content from other pages.
  • 📝 The 'section' directive is used to specify where the content of individual pages should be inserted into the layout.
  • 🗂️ The presenter discusses the benefits of separating the navigation bar into its own file, 'nav.blade.php', for cleaner and more maintainable code.
  • 🔄 The 'include' directive in Blade is demonstrated to incorporate the navigation file into the layout.
  • 🛠 The video also covers how to use Laravel's configuration files and environment variables to manage application settings, such as the app name.

Q & A

  • What is the main focus of today's episode in the video?

    -The main focus of today's episode is to learn more about Lara Blade, bring the HTML template for the project, and create the first layout.

  • Where can the viewers find the HTML template code for the project?

    -The viewers can find the HTML template code on a GitHub repo, with the link provided in the video description.

  • What is the specific HTML file used for this episode?

    -The specific HTML file used for this episode is index.html.

  • What is the role of 'bootswatch' in the project?

    -Bootswatch is a theme for Bootstrap, used in the project to give it a particular design look.

  • What is the purpose of creating a 'terms' page in the script?

    -The purpose of creating a 'terms' page is to provide a simplified terms and conditions page for the application, demonstrating how to add a second page to the project.

  • How does the script handle duplicated content like the header and footer in multiple pages?

    -The script creates a reusable layout that includes a header and footer, and uses the Blade 'yield' directive to insert unique page content without duplication.

  • What is the purpose of the 'yield' directive in Blade templates?

    -The 'yield' directive in Blade templates is used to define a section of content that can be filled in by child templates that extend the layout.

  • What is the benefit of creating a separate 'nav.blade.php' file?

    -Creating a separate 'nav.blade.php' file helps to keep the layout file clean and organized by separating the navigation code from the main layout.

  • How can the application's name be changed using configuration files?

    -The application's name can be changed by modifying the 'app.name' in the '.env' file and then accessing it in the Blade file using the 'config' function.

  • What does the video suggest for managing environment variables and configurations?

    -The video suggests using the '.env' file to store environment variables and the 'config' folder to manage application configurations, which can be accessed within Blade files for dynamic content.

  • How does the video demonstrate the use of the 'include' directive in Blade?

    -The video demonstrates the use of the 'include' directive by including the 'nav.blade.php' file in the layout, which allows for reusing the navigation code across different pages.

Outlines

00:00

📝 Introduction to Lara Blade Project Layout

The video begins with a welcome to a new episode focused on Lara Blade, where the presenter plans to introduce an HTML template for a project and create the initial layout. The template is available on GitHub, with a specific focus on the 'index.html' file. The presenter removes unnecessary elements like the profile page and discusses the use of Bootstrap 5 with Bootswatch for a distinct visual style. The current state of the project includes sections for sharing ideas, a search bar, follow functionality, a comments box, and login/register pages, with plans to implement authentication later. The presenter also demonstrates how to clean the app by creating secondary pages, such as a 'terms and conditions' page, using a simple closure-based route without a controller.

05:02

🛠 Creating a Reusable Layout with Blade

In this section, the presenter addresses the issue of duplicated header and footer code across multiple pages and introduces the concept of creating a reusable layout in Laravel Blade. The process involves creating a 'layout.blade.php' file within a new 'layouts' folder, where the header and footer are defined, excluding the main content section which is intended to be unique per page. The presenter uses Blade's 'yield' directive to define a 'content' section, which is then filled by extending the layout in other Blade files. The video also covers the use of the 'section' directive to replace the 'yield' content with the specific page's HTML. Additionally, the presenter simplifies the layout by separating the navigation bar into a 'nav.blade.php' file and uses the 'include' directive to incorporate it into the main layout.

10:04

🔧 Utilizing Configuration Files in Laravel

The final paragraph discusses the use of configuration files in Laravel, specifically focusing on changing the application's name from 'Laravel' to 'Ideas'. The presenter explains that configurations are stored in the '.env' file and accessed through the 'config' function in Blade files. An example is given on how to change the application name in both the 'app.php' configuration file and the '.env' file, and how to display it within the Blade template using the 'config' directive. The video concludes with a demonstration of how changing the 'app.name' in the '.env' file dynamically updates the application's name displayed on the website, emphasizing the ease of managing configurations in Laravel.

Mindmap

Keywords

💡Lara Blade

Lara Blade is a typo in the transcript, likely referring to 'Laravel Blade', which is a templating engine included with the Laravel PHP framework. It allows developers to write views with plain PHP, making the process of building web pages more efficient and readable. In the video, the creator uses Laravel Blade to build the layout for a project, demonstrating its use in creating reusable components.

💡HTML Template

An HTML template refers to a pre-designed HTML file that serves as a starting point for web development. It includes the basic structure and elements that will be used throughout the website. In the context of the video, the creator discusses using an HTML template for a project, which is then integrated into the Laravel Blade files to create the initial layout.

💡GitHub Repo

A GitHub repository is a location where projects are stored and version-controlled using Git. It allows developers to collaborate on code and track changes. In the video script, the creator mentions a GitHub repository where the code for the project, including the HTML template, is stored and shared with the audience.

💡Bootstrap 5

Bootstrap 5 is a popular front-end framework used for developing responsive and mobile-first websites. It includes a set of CSS and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components. The video mentions using Bootstrap 5 with a 'bootswatch' theme to give the project a distinct visual style.

💡Blade Template

A Blade template in Laravel is a file that contains HTML along with Blade directives and control structures like loops and conditionals. It allows for the creation of dynamic views that can be rendered with data passed from the application. The video script describes the process of creating and using Blade templates for different pages of a web application.

💡CDN

CDN stands for Content Delivery Network, which is a system of distributed servers that deliver web content to users based on their geographic location. It is used to enhance the performance and availability of web resources like CSS and JavaScript files. In the video, the creator uses a CDN to include Bootstrap 5 in the project.

💡Reusable Layout

A reusable layout in web development refers to a design template that can be used across multiple pages of a website to maintain consistency and reduce redundancy. In the video, the creator discusses creating a reusable layout in Laravel Blade to avoid duplicating the header and footer across different pages.

💡Section and Yield

In Laravel Blade, 'section' and 'yield' are directives used to define and render content areas within a layout. 'Section' is used to define a content area, while 'yield' is used to specify where that content should be inserted into the layout. The video explains how to use these directives to create a dynamic layout that can be extended by other Blade files.

💡Include Directive

The 'include' directive in Blade is used to include the content of another Blade file within the current file, similar to the 'include' statement in PHP. It allows for the reuse of code, such as navigation bars or footers, across different views. The video script describes using the 'include' directive to incorporate a navigation bar into the layout.

💡Environment Variables

Environment variables are used to store configuration settings and can be accessed by an application at runtime. They are particularly useful for managing different configurations for various environments (e.g., development, testing, production). In the video, the creator shows how to change the application name in the '.env' file and access it within Blade templates.

💡Configuration Files

Configuration files contain settings and parameters that an application uses to operate. In Laravel, these are typically stored in the 'config' directory and can be accessed using the 'config' helper function. The video script explains how to modify the application name in the configuration files and display it on the web pages using Blade.

Highlights

Introduction of a new episode focused on Larablade and HTML template integration.

Availability of the project's HTML template on GitHub for public use.

Explanation of the main HTML file 'index.html' used for the project layout.

Removal of unused elements like the profile page from the HTML template.

Utilization of Bootstrap 5 and Bootswatch for a distinct visual style.

Demonstration of the current simple HTML template with CDN and JavaScript.

Overview of the initial dashboard layout featuring sharing ideas, search bar, follow, comments, and login/register pages.

Discussion on the potential need for a separate page for comments in the future.

Introduction of creating a 'terms and conditions' page using a closure without a controller.

Use of the same HTML template for the 'terms' page with added dummy text.

Identification of the issue with duplicated headers and footers across pages.

Introduction of creating a reusable layout to avoid duplication of headers and footers.

Explanation of using Blade's 'yield' directive to define a section for page-specific content.

Demonstration of extending the layout in other pages like the dashboard.

Introduction of separating the navigation bar into a separate 'nav.blade.php' file for cleaner code.

Use of the 'include' directive to incorporate the navigation file into the layout.

Explanation of using environment variables and configuration files to manage application settings.

Demonstration of changing the application name using the 'config' function in Blade templates.

Encouragement for viewers to explore the '.env' file and configuration settings on their own.

Conclusion summarizing the creation of the first reusable layout and its benefits.

Transcripts

play00:01

hey guys welcome back to another episode

play00:03

so in today's episode we're going to

play00:04

learn more about uh Lara blade and we

play00:07

are actually going to bring our HTML

play00:09

template for this project and create our

play00:11

first layout so as I mentioned on the

play00:13

first episode we do actually have an

play00:15

HTML template for this project and I

play00:17

have the code on

play00:19

this GitHub repo I'll have the link in

play00:21

the description you guys can go actually

play00:22

use it

play00:24

and the main one we're going to use

play00:26

actually for this episode is going to be

play00:27

index.html so I'm actually going to go

play00:30

ahead and I'm going to copy the HTML

play00:31

Inside It's relatively short

play00:35

and I'm going to paste it in our

play00:36

dashboard.blade file that we created on

play00:38

the previous episode and I have actually

play00:40

gone ahead and removed some of the balls

play00:43

that we are no longer using like the

play00:45

profile page for now

play00:47

so this is our HTML we are using

play00:50

bootstrap 5 so I'm using something

play00:52

called boot Swatch which is like a team

play00:54

for bootstrap

play00:56

to give us that kind of drawing look

play00:59

I'll show you guys in a second

play01:01

and yeah so we have one HTML here we're

play01:03

using the CDN and we have one JavaScript

play01:06

at the end so that's it very simple

play01:08

template so let's go see how it looks

play01:12

and yeah this is how it looks right now

play01:14

uh we have a section for sharing our

play01:16

ideas we have a search bar we can follow

play01:18

people here we have a comments box here

play01:21

which I think later on I will change a

play01:23

little bit

play01:24

I don't know if it's good to have the

play01:26

comments here we might have a separate

play01:27

page for that and we have a login and

play01:29

register page which we will Implement

play01:31

later on once we get into authentication

play01:33

for now we won't be covering that so

play01:37

now that we have this

play01:39

I want to actually show you guys how you

play01:41

can clean this app so what if you had a

play01:44

second page

play01:45

for example

play01:48

we had a terms and conditions page as a

play01:51

matter of fact let's go and create our

play01:52

terms page right now

play01:54

so I'll go to our web.route and I create

play01:58

a terms page

play02:01

and I'll do it the old school way I'm

play02:03

not going to create a controller for it

play02:04

I'm just going to create a closure

play02:08

and I'm going to return a view with the

play02:11

name of terms so I'll do that right now

play02:17

and I'll use the exact same template

play02:23

so I'll close this so you guys can see

play02:25

this is our navigation

play02:28

so this container is basically where the

play02:30

main content of the website is and here

play02:32

I'm going to add

play02:34

a div

play02:36

I suppose

play02:39

did I add two divs

play02:41

I did okay

play02:43

I'll add a div and inside it I'll put

play02:45

some Lauren ipsum so

play02:49

some dummy text and if you guys are not

play02:51

familiar with learn ipsum it's just some

play02:53

basically nonsense data people use as

play02:56

placeholder ticks or maybe it does have

play02:58

meaning I'm not sure actually I don't

play03:00

want to say it's nonsense but that's

play03:02

with commonly used I'll put it here

play03:05

so if we pretend this is our temperature

play03:07

conditions

play03:09

so I'll go to the terms page and you can

play03:12

see it is working right we have a very

play03:15

simplified terms and conditions page and

play03:17

I'll even add

play03:19

an H1 tag

play03:23

so it is working

play03:24

but one issue you might have guessed and

play03:27

this is a common issue when you're also

play03:28

writing with PHP right vanilla PHP is

play03:31

ever hitter and our footer is duplicated

play03:33

on each of the pages we make right so

play03:35

throughout this application you might

play03:37

have like 10 pages and we don't want to

play03:39

actually have to copy and paste this

play03:41

header what if you want to change this

play03:42

title right do you have to do it like 10

play03:44

times what if you want to change our you

play03:47

know template whatever things like that

play03:49

so what we need to do is actually create

play03:51

a reusable layout for all our pages so

play03:54

let's go about let's go and do that

play03:57

I'm going to go ahead and actually

play03:58

inside our reviews folder create a new

play04:00

an extra folder I'm going to call it

play04:04

layout yeah

play04:07

and inside here I'll name another file

play04:10

also named layout you could also name

play04:13

your folder shared if you like but I

play04:15

call it layout Dot blade.php

play04:21

so inside here I'm going to go ahead and

play04:24

I'm going to actually paste in our

play04:26

header and footer except for this

play04:29

section which is

play04:31

for a specific page right so that one is

play04:34

going to be unique to each page and it's

play04:36

not going to be duplicated and I'll do

play04:38

the same for the footer so

play04:41

I'm gonna move it here

play04:45

and I'll format it so it looks nice

play04:48

and I'm going to add some comments

play04:50

and with lower blade you can actually

play04:52

add comments kind of similar with PHP so

play04:54

this is how comments look you have two

play04:57

open and curl braces Dash Dash and again

play04:59

dash dash two closing curve bases this

play05:02

is going to be comments in Blade

play05:04

and I'm going to say page content goes

play05:08

here right just so we have some

play05:10

indicator here

play05:12

so if we go save this go back to our

play05:14

terms page obviously it's going to look

play05:16

awful to be expected so what I want to

play05:18

do is instead of having to write this

play05:22

header and footer HTML all the time I

play05:24

want to tell Lara blade hey go and fetch

play05:27

it from this layout file right and put

play05:29

our terms data this HTML inside this

play05:34

container how do we go about doing that

play05:36

so lower blade actually has a directive

play05:39

for this it's called the yield

play05:41

and basically you create a section and I

play05:44

name it content

play05:47

and then you're able to actually go to

play05:49

another blade file such as our terms

play05:51

page and tell it to extend

play05:54

extends layout

play05:58

so now it's going to go ahead and

play06:00

actually copy all the code from

play06:03

our layout.php and put it inside these

play06:06

terms at PHP so I'm just going to go

play06:08

ahead and refresh the page we do get a

play06:10

error here

play06:13

because I made a mistake with the name

play06:14

so we we need to go layout dot layout

play06:18

right it's a layout.blade inside the

play06:20

layout folder

play06:24

so and the convention here we could also

play06:26

use this but I would like to use dot

play06:28

that's the convention in the PHP

play06:29

community so

play06:31

if you refresh and you can see this is

play06:32

actually kind of working now it does

play06:34

look out of back and I'll tell you guys

play06:35

why

play06:36

right now now that we have extended it

play06:39

Lara blade doesn't know where to put

play06:41

this HTML right

play06:43

for us we want to put it inside this div

play06:46

right

play06:47

that's why we put the yield yield tag

play06:50

here or the yield directive so in order

play06:53

to actually move this HTML inside

play06:56

this container what we can do is we can

play06:58

wrap it around a directive called

play07:01

section

play07:02

and we need to name it same as ever

play07:04

yield so here I named it content I'm

play07:06

going to do the same here

play07:10

and by doing so we are telling Lara

play07:13

blade hey go ahead copy this HTML

play07:16

and move it inside

play07:19

here or replace it with this yield

play07:21

that's what we are basically telling

play07:23

Lara blade to do so if we refresh our

play07:25

page we can see now it looks very nice

play07:28

it looks exactly like it did previously

play07:30

right

play07:32

so I'm going to go ahead and do the

play07:34

exact same thing on our dashboard page

play07:36

I'll get rid of all the duplicated HTML

play07:39

so the footer I'll remove it

play07:42

I remove this I'll remove

play07:45

all of that

play07:48

and I'm going to say extends layout dot

play07:52

layout

play07:57

and here I'm going to say section

play07:58

content

play08:01

again don't forget about the section

play08:03

otherwise your page is going to look

play08:05

weird

play08:07

I'm going to save it

play08:09

go back

play08:10

and if you go to our root page you can

play08:12

see it also looks like it did before so

play08:14

we have basically created our first

play08:16

reusable layout in

play08:20

our lower application now one more thing

play08:22

I would like to do is actually I want to

play08:24

simplify this a little bit more

play08:27

I actually like to separate my

play08:29

navigation this navigation bar

play08:33

from this layout file and the reason I

play08:35

like to do that is

play08:36

later on we're going to have a lot of

play08:38

Blade code inside here we're going to

play08:40

have our you know different Navigation

play08:41

pages I like this to be a bit more clean

play08:44

so what I'm going to do is I'm going to

play08:45

create

play08:46

in new file I'm going to call it

play08:49

nav.blade.php for now

play08:51

and I'm going to move our navigation

play08:54

inside that file so I'll cut this I'll

play08:57

go inside nav

play08:59

and in order to actually use our

play09:03

navigation file here we can use

play09:05

something called include the include

play09:07

directive and I'm going to say layout

play09:09

dot nav and the include directive is

play09:12

same as copying where basically or

play09:14

similar to include

play09:15

inside PHP right when you include

play09:18

another PHP file we're basically telling

play09:19

Lara blade hey go ahead find this nav

play09:22

file which is here copy all of its

play09:24

content and paste it here

play09:26

it's same as that and so this makes our

play09:29

code a bit more reusable later on if we

play09:31

wanna

play09:33

maybe use this navigation in a different

play09:35

layout if you want or if this nav fault

play09:38

gets very long

play09:40

it still keeps our layout file clean and

play09:43

small

play09:45

so I'll go save it and it looks like it

play09:47

did before no issues

play09:49

now I also want to teach you guys one

play09:50

more thing and that is about how to

play09:52

actually use our configuration files

play09:56

so here we have Twitter clone bootstrap

play09:58

5 template I actually want to change

play10:00

this with the name of our application

play10:01

now you can technically just type in

play10:03

ideas

play10:05

but larval comes in with a way to store

play10:09

the configuration so

play10:12

and your configuration are actually

play10:13

stored environment variables are stored

play10:15

inside that EnV file if you go here find

play10:19

the dot in v file you see a bunch of

play10:21

configurations here and we'll learn

play10:23

about them as we go along but we're now

play10:25

at the top you have something called app

play10:27

name

play10:28

so that actually is the app name of your

play10:30

application I'm going to actually change

play10:31

that to ideas

play10:33

so that's going to be our app name from

play10:35

now on

play10:37

and

play10:39

the where this app name is actually

play10:41

defined inside this config folder where

play10:43

I mentioned in the episode three so if

play10:46

you go inside config click on the first

play10:48

one app so these are going to be

play10:50

application related configuration

play10:53

you will see that it's actually defined

play10:55

here right

play10:57

so this EnV function if you haven't used

play11:01

it is I believe a public library you can

play11:03

also use it in PHP all it does is read

play11:07

this app name from our EnV file so from

play11:10

this EnV file which stands for

play11:12

environment variables

play11:14

is reading it from this EnV file so

play11:16

that's why it's called EnV let's read it

play11:19

from EnV if it's not available

play11:21

use laurable as default so I'll show you

play11:24

guys the

play11:25

definition it tries to find a key inside

play11:28

our inverify environment variable file

play11:30

if it can't find it it'll use the

play11:32

default and so that's why the default

play11:34

app name is laurable if you don't have

play11:36

this EnV fault but we do

play11:38

and for us it's called ideas

play11:42

now how can we actually access this

play11:44

configuration inside our blade file

play11:46

well luckily for us it's actually very

play11:48

easy I'll open the kind of Blade display

play11:52

tag or directive right which is the

play11:54

opening curl braces and I'm going to

play11:57

type in config yeah it's that simple the

play11:59

config function

play12:01

and then you need to name give the name

play12:04

of the config so as you can see inside

play12:05

this config folder we have a bunch of

play12:07

them right we have I don't know maybe

play12:08

it's 12 or something

play12:10

you first type in the name of your

play12:12

config which is app

play12:14

you put dot and then the specific config

play12:17

that you want so inside our

play12:19

app.phpconfig

play12:21

we have the name configuration we have

play12:23

the EnV configuration we have debug

play12:25

right we have the time zone

play12:26

configuration we have a bunch of them

play12:28

for now I want the name configuration so

play12:31

I'm just going to type in name

play12:32

right so a lot of people go ahead inside

play12:34

the app and take this name and

play12:37

return it to us here so I'm going to

play12:39

save this go back

play12:42

and I'm not sure if it's visible but at

play12:44

the top left we have ideas over here

play12:48

we also want to change this with the

play12:49

name in our own environment variable or

play12:51

our configuration

play12:53

so I'll copy this and find it inside our

play12:56

navigation layout.nav

play12:59

where is the ideas oh it's here so it's

play13:01

hard coded here I'll replace here here

play13:03

as well

play13:06

and if you refresh you get ideas that's

play13:09

it I think I left the extra space

play13:11

so I'll get rid of that

play13:13

and as you can see we get ideas and just

play13:16

to show you it does work I'll change it

play13:17

from ideas to maybe

play13:19

something else

play13:22

hello

play13:26

and you can see it is actually working

play13:28

so we are loading this from our

play13:29

environment variable and that's the very

play13:31

basics of configurations we will learn

play13:33

more about configurations in later

play13:35

episodes but I just wanted to show you

play13:36

guys a little bit of a small snippet so

play13:40

you guys can actually play around with

play13:41

this if you like

play13:43

and you can also maybe go through this

play13:45

dot EnV file on your own time and see

play13:47

what's available we can see we have our

play13:49

database connection information our log

play13:52

information and some additional stuff

play13:54

here we will

play13:55

work with them later on

play13:57

CE we are we are going to use our

play14:00

database but for now this is all we need

play14:02

and yeah that's it guys so

play14:04

we have created our first layout I may

play14:07

rename these two later on but for now

play14:09

this is good

play14:11

and our page is working so hope you guys

play14:13

enjoyed this episode if you have any

play14:14

questions we can leave them in the

play14:15

comment sections below and I try to

play14:18

answer all of you guys question also

play14:20

don't forget to like And subscribe for

play14:22

more content

play14:23

see you guys on the next episode have a

play14:24

great day

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Web DevelopmentHTML5Bootstrap 5Laravel BladeTemplate LayoutWeb DesignCoding TutorialApp CreationBlade DirectivesEnvironment Config
¿Necesitas un resumen en inglés?