Site Essentials for Statamic

Site Essentials for Statamic

se_capture Tag

The se_capture tag can be used to store the results of a template section, and output it later as a string. This is particularly helpful if you want to reuse the results of a partial, or navigation menu in multiple places without having to render the template code twice.

To capture a section of a template we must use the se_capture tag as a tag pair. The "method part" will become the name of the captured section. In the following example, we will capture the results of a partial tag and store it as "menu":

1{{ se_capture:menu }}
2 {{#
3 Our partial tag which might
4 have some expensive nav code.
5 #}}
6 {{ partial:navs/main /}}
7{{ /se_capture:menu }}

Later on in our template, we can reuse the contents by using the se_capture tag without the closing tag (it is good practice to use self-closing tags when working with the se_capture tag in this way):

1{{# Output our captured region multiple times. #}}
2{{ se_capture:menu /}}
3 
4{{ se_capture:menu /}}

An important thing to keep in mind is we can only output captured regions after the tag pair has been evaluated.

All contents captured by se_capture will be removed after the rendering process has completed, making it safe to use with the static site generator.

#Immediately Rendering Captured Content

Sometimes we want to capture the content to be used later, but also render it at the current point in the template. Typically, we would do this like so:

1{{ se_capture:menu }}
2 {{#
3 Our partial tag which might
4 have some expensive nav code.
5 #}}
6 {{ partial:navs/main /}}
7{{ /se_capture:menu }}
8 
9{{# Output the captured content. #}}
10{{ se_capture:menu /}}

Instead of calling the se_capture tag twice, we can simply add the output="true" parameter. This will cause the output to be rendered, but also capture the content:

1{{ se_capture:menu output="true" }}
2 {{#
3 Our partial tag which might
4 have some expensive nav code.
5 #}}
6 {{ partial:navs/main /}}
7{{ /se_capture:menu }}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.