# Template Syntax Reference > Complete reference for Dynamic Content Block template syntax, including data access, loops, formatting, and troubleshooting. This page covers everything you need to know about writing templates for Dynamic Content Blocks, from basic data access to advanced techniques. ## Accessing Data Fields ### Basic field access Reference your data fields using double curly braces with backticks for field names: ```aml {{ rows[0].`Product Name` }} {{ rows[0].`Revenue` }} ``` - `rows` is an array of all data rows returned by your query - `rows[0]` accesses the first row, `rows[1]` the second, and so on - You can also use dot notation: `rows.0` is equivalent to `rows[0]` - Use backticks around field names, especially those with spaces ### Negative indexing Access rows from the end using negative indices: ```aml {{ rows[-1].`Product Name` }} // Last row {{ rows[-2].`Product Name` }} // Second to last row ``` ### Raw and formatted values Each field has two value types you can access: ```aml // Get raw value (unformatted) {{ rows[0].`Field Name`.raw }} // Get formatted value (with formatting applied) {{ rows[0].`Field Name`.formatted }} ``` **When to use `.raw`:** - URLs for `` or `` - Values for inline CSS styles (e.g., percentages for width) - CSS class names derived from data values - Values without link formatting **When to use `.formatted`:** - Display values with number formatting, currency symbols, etc. - Dates with specific format applied **Default behavior (no suffix):** When you use ``{{ rows[0].`Field Name` }}`` without `.raw` or `.formatted`, it returns a clickable drill component with the formatted value as the label. This enables cross-filtering when users click on the value. ### Aggregated values (metrics) Access aggregated metrics using the `values` accessor: ```aml // Get all metric values for a row {{ rows[0].values }} // Get a specific metric value {{ rows[0].values.`Total Revenue` }} {{ rows[0].values.`Average Order Value` }} ``` ## Looping Through Rows ### Basic loop Use the `map` function to iterate through all rows: ```aml {% map(rows) %} {{ `Product Name` }}: {{ `Revenue` }} {% end %} ``` **Important**: Inside a `map` loop, reference fields directly without the `rows[0]` prefix. ### Accessing values inside loops Inside a loop, you can access field values and metrics directly: ```html {% map(rows) %} {{ `Product Name` }} {{ values.`Revenue` }} {% end %} ``` ### Loop example: product list ```html {% map(rows) %} {{ `Product Name` }} - {{ values.`Revenue` }} {% end %} ``` ### Advanced looping for pivot data For pivot-style layouts (like heatmaps or matrices), you can loop through columns and cell values: **`{% map(columns) %}`** - Iterate over column headers: ```html {% map(columns) %} {{ `Month Number` }} {% end %} ``` **`{% map(values) %}`** - Inside a row loop, iterate over cell values: ```html {% map(rows) %} {{ `Cohort Month` }} {% map(values) %} {{ value.`Total Users` }} {% end %} {% end %} ``` **Complete example - Retention Heatmap:** ```html Cohort {% map(columns) %} {{ `Month Number` }} {% end %} {% map(rows) %} {{ `Cohort Month` }} {% map(values) %} {{ value.`Total Users` }} {% end %} {% end %} ``` Inside `{% map(values) %}`, use `` value.`Metric Name` `` to access specific metrics for each cell. ### Accessing pivot total values When working with pivot tables, you can access aggregated totals at three levels: per column, per row, and for the entire dataset (grand total). These are useful for rendering summary rows or footer totals in your template. **Column Totals** Use `col_totals` to access the total values for each pivot column. You specify the column value first, then the metric name: ```aml // All column totals {{ col_totals }} // Total for a specific column {{ col_totals.`cancelled`.`Revenue` }} ``` You can also iterate through all column totals: ```html {% map(col_totals) %} {{ value.`Revenue` }} {% end %} ``` Note: `col_totals` is a key-value object, not an array, so you must use ``{{ value.`Field Name` }}`` inside the map. **Row Totals** Access the total across all columns for a specific row using `row_total` on that row: ```aml {{ rows.0.`row_total`.`Revenue` }} ``` Inside a `map(rows)` loop, you can access the row total directly: ```html {% map(rows) %} {% map(values) %} {{ value.`Revenue` }} {% end %} {{ `row_total`.`Revenue` }} {% end %} ``` **Grand Totals** Access the grand total for the entire dataset: ```aml {{ grand_totals.`Revenue` }} ``` ## Interactive Elements ### Clickable element for cross-filtering and drill To create clickable elements that filter other dashboard components, wrap them with the `` tag: ```html {% map(rows) %} {{ `Country Icon`.formatted }} {{ `Country`.formatted }} {% end %} ``` - The `` tag wraps the clickable element - Use `.raw` for the `value` attribute (the actual filter value) - Use `.formatted` for display text inside the element ### Styling active state for selected data point When a user clicks on a data point or a drill element, the `.h-drill-selected` class is added directly to the child element inside ``. Use this class to style the active state: ```html ``` Note: The selector is `.h-drill-selected.clickable-card` (both classes on the same element), not `.h-drill-selected > .clickable-card`. ### Paginate large result sets By default, a Dynamic Content Block paginates its data, splitting a large result set into multiple pages so users aren't overwhelmed by everything at once. **Add previous/next controls** Wrap any clickable element, such as text, a button, or an icon, with `` and set `type` to move between pages: ```html Previous Next ``` **Show the current page** Use `{{ meta.page }}` and `{{ meta.page_count }}` in your Markdown content to display which page the user is on: ```aml Page {{ meta.page }} of {{ meta.page_count }} ``` **Set a page size** Each page shows a max of 25 records by default. Set `pagination_size` in `settings` to change it: ```aml Dashboard dashboard_name { ... block dynamic_content_block: VizBlock { label: 'Product Conversion Performance' viz: MarkdownViz { ... settings { pagination_size: 3 } } } } ``` :::warning Pages aren't synced Despite sharing the same number of pages, changing the pager on the Pivot Table doesn't move the MarkdownViz's page, and vice versa, so treat them as two separate controls. ::: ## Tips and Tricks ### Conditional styling with CSS classes Apply CSS classes dynamically based on data values: ```html {{ rows[0].`Status` }} ``` This works when your `Status` field contains values like "active" or "inactive" that match your CSS class names. ### Working with images Always use `.raw` for image URLs: ```html ``` Inside a loop: ```html {% map(rows) %} {% end %} ``` ### Creating links Build dynamic links with data values: ```html {{ rows[0].`Email` }} View Details {{ rows[0].`Location` }} ``` ### Using raw values in CSS When you need data values in inline styles (like positioning or sizing), you must use `.raw`: ```html ``` ## Best Practices 1. **Start with your data**: Build and validate your dataset first using the Chart Data preview before creating your template 2. **Use the Chart Data tab**: Preview your data structure to understand field names and values 3. **Test with real data**: Use the live preview to see how your template renders with actual data 4. **Keep CSS scoped**: Avoid generic class names like `.title`, `.card`, `.button` that could conflict with Holistics app's styles or other dashboard blocks. Instead, use prefixed or specific names: - ❌ Bad: `.title`, `.card`, `.button`, `.wrapper`, `.container` - ✅ Good: `.custom-metric-title`, `.product-card`, `.mkt-insight-button`, `.dcb-wrapper`, `.deal-container` 5. **Handle missing data**: Test your template with incomplete data to ensure it fails gracefully 6. **Optimize performance**: For large datasets, consider limiting rows or using filters to reduce data volume 7. **Mobile responsive**: Use responsive CSS techniques if your dashboard will be viewed on mobile devices ## Troubleshooting ### Template not rendering If your template isn't showing up: - Check for syntax errors in your HTML/Markdown - Ensure field names match exactly (case-sensitive) with backticks - Verify your data query returns results in the Chart Data tab ### Data not displaying If data fields show as empty: - Confirm field names use backticks: `` `Field Name` `` - Check if you need `.raw` for unformatted values - Verify the row index exists (e.g., `rows[0]` requires at least one row) ### Styling conflicts If your styles aren't applying: - Use specific CSS class names to avoid conflicts - Check browser developer tools for CSS specificity issues - Ensure your `