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!
Macros are comparable with functions in regular programming languages. They are useful to reuse often used HTML fragments to not repeat yourself.
Macros are comparable with functions in regular programming languages. They are useful to reuse often used HTML fragments to not repeat yourself.
A macro is defined via the macro tag. Here is a small example (subsequently called forms.html) of a macro that renders a form element:
{% macro input(name, value, type = "text", help) %}
<div class="form-group">
<label for="input{{ name }}">{{ name|title }}</label>
<input class="form-control" type="{{ type }}" id="input{{ name }}" name="{{ name }}" value="{{ value|e }}" placeholder="Enter your {{ name }}" />
{% if help %}<small class="form-text text-muted">{{ help }}</small>{% endif %}
</div>
{% endmacro %}
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
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