Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Templating with Twig!
You have completed Templating with Twig!
Preview
Macros can be defined in any template, and need to be "imported" via the import tag before being used.
Macros can be defined in any template, and need to be "imported" via the import tag before being used:
{% import "forms.twig" as forms %}
<form method="post">
{{ forms.input('name', post.name) }}
{{ forms.input('email', post.email, 'text', "We'll never share your email with anyone else.") }}
{{ forms.textarea('message') }}
<button type="submit" class="btn btn-primary">Send Message</button>
</form>
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We can close base and home.
0:00
We're going to add a form tag,
we're gonna set the method = post.
0:04
And we'll let it submit back here for now.
0:10
Form, and we'll add our submit button.
0:13
Submit, class= btn btn-primary,
0:21
and then Send Message, button.
0:29
Okay, before our form,
0:35
we're going to import our macros.
0:39
So we use a command to import
0:44
forms.twig as forms.
0:50
This is how we access our macro.
0:55
Now within our form,
we'll use forms like an object name and
0:58
then we'll call them macro.
1:04
Forms.input and then we'll pass in name,
1:06
we named our file form, not forms.
1:12
Let's rename the file.
1:16
Great, we have a form
that posts to itself,
1:21
with a form field named name.
1:26
Let's go back and add additional options.
1:30
The next argument is a value, we can't
use our post variables directly in Twig.
1:33
So we'll have to pass those
variables into our template.
1:40
Let's go into index.php.
1:44
And then in our past array, we're going
to add a new element called host.
1:47
And we'll set this equal
to our $_POST values.
1:54
Now back in our contact template,
we can use for
1:59
our next argument, post.name.
2:05
Let's try this in the browser again.
2:08
Great, our form field
keeps the entered data.
2:14
The next argument is the type.
2:17
I can pass nothing,
an empty string, or null, and
2:20
they will all default to
using that default value.
2:24
I can also specify my type as text or
anything else I want.
2:28
I'm going to pass an empty string.
2:33
My final argument is the help,
I don't want use anything for name.
2:36
So I'm actually just going to leave
off these last two arguments.
2:42
I'm going to copy this line for email.
2:47
My first argument will be email and
2:53
then post.email,
this time pass an empty string.
2:57
And then for help I want to add,
3:02
we will never share
your email with anyone.
3:07
Let's take a look in the browser.
3:13
This time, we have a name and
an email field.
3:18
And our email field
shows our message below,
3:21
we will never share
your email with anyone.
3:24
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up