<mvt:capture>
#
The <mvt:capture>
tag in Miva Merchant’s template language allows you to capture the evaluated content within its tags and assign it to a variable. This makes it a powerful tool for dynamically generating and storing complex data, such as JSON strings, HTML fragments, or processed results, for reuse throughout your templates.
Available on any page without requiring item assignments, <mvt:capture>
is a versatile solution for dynamic content generation.
Empresa Requirement
Requires Miva Empresa Engine 5.33 or higher.
Syntax#
<mvt:capture variable="variable_name">
<!-- Content to evaluate and save -->
</mvt:capture>
Attributes#
Attribute | Description |
---|---|
variable |
Required. The name of the variable to store the captured content. |
Examples#
Capture a JSON String#
Capture a JSON string and store it in a variable for later use.
<mvt:capture variable="g.json_data">
{
"Store_Code": "{{Store_Code}}",
"Miva_Request_Timestamp": "{{$timestamp}}",
"Function": "Module",
"Module_Code": "sample",
"Module_Function": "Export_Products"
}
</mvt:capture>
<p>Captured JSON:</p>
<pre>&mvt:global:json_data;</pre>
Output:
{
"Store_Code": "{{Store_Code}}",
"Miva_Request_Timestamp": "{{$timestamp}}",
"Function": "Module",
"Module_Code": "sample",
"Module_Function": "Export_Products"
}
Capture and Process an Expression#
Capture the result of an evaluated mathematical expression.
<mvt:capture variable="g.result">
<mvt:eval expr="(10 + 5) / 3" />
</mvt:capture>
<p>Captured Result: &mvt:global:result;</p>
Output:
Captured Result: 5
Generate and Reuse HTML Content#
Capture dynamically generated HTML content for reuse in multiple locations.
<mvt:capture variable="g.banner">
<div class="banner">
<h1>Welcome to Our Store!</h1>
<p>Enjoy free shipping on orders over $50.</p>
</div>
</mvt:capture>
<!-- Display the banner in multiple places -->
<div class="header">
&mvt:global:banner;
</div>
<div class="footer">
&mvt:global:banner;
</div>
Output:
<div class="banner">
<h1>Welcome to Our Store!</h1>
<p>Enjoy free shipping on orders over $50.</p>
</div>
Nested Captures#
Use nested <mvt:capture>
tags to build complex data structures.
<mvt:capture variable="g.product_data">
<mvt:capture variable="g.product_name">
<mvt:eval expr="'Product A'" />
</mvt:capture>
{
"name": "&mvt:global:product_name;",
"price": 19.99,
"stock": 50
}
</mvt:capture>
<p>Captured Product Data:</p>
<pre>&mvt:global:product_data;</pre>
Output:
{
"name": "Product A",
"price": 19.99,
"stock": 50
}
Best Practices#
-
Use Meaningful Variable Names:
- Assign descriptive names to captured variables to improve readability and maintainability.
-
Avoid Excessive Nesting:
- While nesting
<mvt:capture>
tags is powerful, excessive nesting can reduce readability.
- While nesting
-
Comment Complex Captures:
- For complex data structures or logic within captures, include comments to explain their purpose.
-
Reusability:
- Use
<mvt:capture>
to store content or data that will be reused multiple times in your template.
- Use
-
Validate Output:
- Always test captured content to ensure it is correctly formatted and meets your requirements.
Advanced Example#
Capture API Request Payload
Create a dynamic JSON payload for an API request.
<mvt:assign name="g.store_code" value="'STORE123'" />
<mvt:assign name="g.timestamp" value="'2025-01-01T12:00:00Z'" />
<mvt:assign name="g.product_id" value="'ABC123'" />
<mvt:capture variable="g.api_payload">
{
"store_code": "&mvt:global:store_code;",
"timestamp": "&mvt:global:timestamp;",
"action": "get_product",
"product_id": "&mvt:global:product_id;"
}
</mvt:capture>
<p>API Payload:</p>
<pre>&mvt:global:api_payload;</pre>
Output:
{
"store_code": "STORE123",
"timestamp": "2025-01-01T12:00:00Z",
"action": "get_product",
"product_id": "ABC123"
}
Summary#
The <mvt:capture>
tag is an essential tool for capturing and reusing evaluated content in Miva templates. Whether you’re generating JSON payloads, storing dynamic HTML, or processing expressions, <mvt:capture>
enables you to efficiently manage and reuse content.