You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _component/macros/index.md
+195-1
Original file line number
Diff line number
Diff line change
@@ -3,4 +3,198 @@ title: Macros
3
3
layout: component
4
4
path_slug: macros
5
5
category: twig
6
-
---
6
+
---
7
+
8
+
We love Twig and have created a few Macros that help make things a bit easier for us. One thing to note when working with Macros and Twig is that you need to import the macro everywhere you need it. If you use an `embed` or `block` within your Twig file, you need to call the Macro import within that `embed` or `block`. You can't add a macro import at the top of the file and expect to use it wherever.
If you need to work with responsive images use our nifty responsive macro. You can create fixed width images that will generate retina read markup or variable width images with multiple src sets.
14
+
15
+
16
+
### Fixed Width Images
17
+
This macro takes 3 parameters:
18
+
19
+
1. Integer: Image's ID
20
+
2. Integer: The width that you want the image to be displayed in
21
+
3. Object: Object that takes an alt and class value for outputting the markup
22
+
23
+
```twig
24
+
{% raw %}{% import '_macros/_img.twig' as m_img %}
{% if (assetNativeWidth <= params['defaultWidth']) %}
122
+
src="{{assetUrl}}"
123
+
{% else %}
124
+
src="{{assetUrl|resize(width)}}"
125
+
{% endif %}
126
+
127
+
srcset="{{srcset|join(', ')}}"
128
+
sizes="{{params['sizes']|join(', ')}}"
129
+
130
+
alt="{{options.alt}}"
131
+
132
+
{{m_classAttr.classAttr(options.class)}}
133
+
/>
134
+
135
+
{% endmacro %}{% endraw %}
136
+
```
137
+
138
+
## SVG Icons
139
+
Just like the image macro the SVG macro will output an inline SVG element for you. There are a few requirements to use this macro. You must compile all of your SVG files into a single SVG sprite file with specific ID for each single SVG Icon. We use IcoMoon to generate our SVG icons store them in a directory within the theme and then use Gulp to generate the sprite for us.
140
+
141
+
The SVG Macro takes an object as its parameter with the required key value of `icon` with the file name of the SVG icon passed through as the value.
142
+
143
+
```twig
144
+
{% raw %}{% import '_macros/_svg.twig' as m_svg %}
0 commit comments