Skip to main content Link Search Menu Expand Document (external link)

@data-template : The Inline Template Content Attribute

The data-template attribute allows you to define template content inline as a string attribute value, rather than as child elements. This provides a convenient way to specify simple templates, dynamically generate template content, or create templates from external sources.


On this page

Summary

The data-template attribute enables inline template definition, providing:

  • Inline HTML content specification as an attribute value
  • Dynamic template generation from code or external sources
  • Simplified syntax for simple templates
  • Programmatic template creation without DOM manipulation

This attribute is useful for:

  • Simple conditional templates with minimal content
  • Dynamically generated template content
  • Templates loaded from databases or external files
  • Programmatic document generation
  • Templates with content determined at runtime

Note: When data-template is specified, it takes precedence over child elements within the template or conditional component.


Usage

The data-template attribute is applied to <if> elements and can be used with <template> elements (via the data-content attribute synonym):

<!-- Simple conditional template -->
<if data-test=""
    data-template="<div style='color: red;'>Important message</div>">
</if>

<!-- On template element (use data-content) -->
<template data-bind=""
          data-content="<div></div>">
</template>

Basic Syntax

<!-- Conditional rendering with inline template -->
<if data-test=""
    data-template="<p>HTML content here</p>">
</if>

<!-- Bound value for dynamic template -->
<if data-test=""
    data-template="">
</if>

<!-- Template element (use data-content attribute) -->
<template data-bind=""
          data-content="<span></span>">
</template>

Supported Elements

The data-template attribute is supported on the following elements:

  • <if> - Conditional rendering element

Note: For <template> elements, use the synonym attribute data-content instead.


Binding Values

Attribute Values

Value Type Description
HTML String Literal HTML markup as a string
Binding Expression Expression that evaluates to HTML string: ``

Type

Type: string (HTML markup) Default: null (use child elements)

Expression Support

The attribute accepts literal HTML strings or binding expressions:

<!-- Literal HTML -->
<if data-test=""
    data-template="<div class='alert'>Alert content</div>">
</if>

<!-- Bound from model -->
<if data-test=""
    data-template="">
</if>

<!-- Conditional expression -->
<if data-test=""
    data-template="">
</if>

Notes

Precedence Over Child Elements

When data-template is specified, it takes precedence over any child elements:

<!-- This child content is IGNORED -->
<if data-test=""
    data-template="<p>Inline template</p>">
    <p>This content will NOT be rendered</p>
</if>

<!-- Rendered output will be: <p>Inline template</p> -->

HTML Escaping

HTML content within the attribute value must be properly escaped:

<!-- Single quotes inside double quotes -->
<if data-test=""
    data-template="<div style='color: red;'>Text</div>">
</if>

<!-- Or use HTML entities -->
<if data-test=""
    data-template="<div style=&quot;color: red;&quot;>Text</div>">
</if>

Data Binding Within Templates

Inline templates support full data binding syntax:

<if data-test=""
    data-template="<div><strong></strong> - </div>">
</if>

Namespace Handling

When used within HTML documents, the inline template content inherits the HTML namespace automatically:

<html xmlns='http://www.w3.org/1999/xhtml'>
<body>
    <if data-test=""
        data-template="<div>Content</div>">
    </if>
    <!-- Div element has HTML namespace automatically -->
</body>
</html>

Dynamic Template Loading

Templates can be loaded from external sources:

<!-- Model: { condition: true, templateContent: "<div>Loaded content</div>" } -->
<if data-test=""
    data-template="">
</if>

Multi-Line Content

For readability, use appropriate quoting:

<if data-test=""
    data-template="<div style='padding: 10pt; border: 1pt solid black;'>
                      <h3>Title</h3>
                      <p>Content goes here</p>
                   </div>">
</if>

Performance Considerations

  • Inline templates are parsed once during document initialization
  • No significant performance difference vs. child elements
  • Useful for programmatic generation to avoid DOM manipulation
  • Cache template strings when generating many similar templates

Comparison with Child Elements

Use inline templates when:

  • Content is simple and fits in one line
  • Template is generated dynamically
  • Content is loaded from external source
  • Programmatic generation is easier

Use child elements when:

  • Content is complex or multi-line
  • Better readability is desired
  • IDE support for HTML editing is important
  • Content contains many nested elements

Examples

1. Simple Conditional Message

Display a simple message conditionally:

<if data-test=""
    data-template="<div style='color: red; font-weight: bold;'>Error occurred!</div>">
</if>

2. Dynamic Content from Model

Load template content from model property:

<!-- Model: { showNotice: true, noticeHtml: "<div class='notice'>Important notice</div>" } -->
<if data-test=""
    data-template="">
</if>

3. Conditional Badge

Display different badge styles:

<if data-test=""
    data-template="<span style='background-color: red; color: white; padding: 5pt; border-radius: 3pt;'>URGENT</span>">
</if>

<if data-test=""
    data-template="<span style='background-color: green; color: white; padding: 5pt; border-radius: 3pt;'>NORMAL</span>">
</if>

4. Inline Alert Box

Conditional alert with styling:

<if data-test=""
    data-template="<div style='background-color: #fff3cd; border: 1pt solid #ffc107; padding: 15pt; margin: 10pt 0;'>
                      <strong>Warning:</strong> 
                   </div>">
</if>

5. Dynamic Icon Display

Show different icons based on status:

<if data-test=""
    data-template="<span style='color: green; font-size: 14pt;'>✓ Complete</span>">
</if>

<if data-test=""
    data-template="<span style='color: orange; font-size: 14pt;'>⧗ In Progress</span>">
</if>

6. Programmatically Generated Template

Generate template content in code:

<!-- Model: { showDetails: true, detailsHtml: GenerateDetailsHtml() } -->
<if data-test=""
    data-template="">
</if>
// C# code to generate template
string GenerateDetailsHtml()
{
    var sb = new StringBuilder();
    sb.Append("<div style='padding: 10pt;'>");
    sb.Append("<h3>Generated Details</h3>");
    sb.Append("<ul>");
    foreach (var item in items)
    {
        sb.AppendFormat("<li>{0}</li>", item);
    }
    sb.Append("</ul>");
    sb.Append("</div>");
    return sb.ToString();
}

7. Database-Loaded Template

Load template content from database:

<!-- Model: { showCustomSection: true, customHtml: LoadFromDatabase("section_template") } -->
<if data-test=""
    data-template="">
</if>

8. Conditional Pricing Display

Show pricing with conditional formatting:

<if data-test=""
    data-template="<div style='text-align: center;'>
                      <div style='text-decoration: line-through; color: #999;'>$</div>
                      <div style='color: red; font-size: 16pt; font-weight: bold;'>$</div>
                      <div style='color: red; font-size: 10pt;'>Save %!</div>
                   </div>">
</if>

Add conditional footer information:

<if data-test=""
    data-template="<div style='margin-top: 20pt; padding: 10pt; background-color: #f8d7da; border: 2pt solid #dc3545;'>
                      <strong style='color: #dc3545;'>CONFIDENTIAL:</strong>
                      This document contains proprietary information.
                   </div>">
</if>

10. Inline User Badge

Display user type badge:

<div>
    User: 
    <if data-test=""
        data-template="<span style='background-color: #dc3545; color: white; padding: 3pt 8pt; margin-left: 5pt; font-size: 8pt; border-radius: 3pt;'>ADMIN</span>">
    </if>
    <if data-test=""
        data-template="<span style='background-color: #ffc107; color: black; padding: 3pt 8pt; margin-left: 5pt; font-size: 8pt; border-radius: 3pt;'>PREMIUM</span>">
    </if>
</div>

11. Conditional Watermark

Add watermark for draft documents:

<if data-test=""
    data-template="<div style='position: absolute; top: 50%; left: 50%; transform: rotate(-45deg);
                               font-size: 72pt; color: rgba(255,0,0,0.2); font-weight: bold;'>
                      DRAFT
                   </div>">
</if>

12. Simple Table Row

Conditional table row with inline template:

<table style="width: 100%;">
    <tr>
        <td>Order Status:</td>
        <td>
            <if data-test=""
                data-template="<span style='color: green; font-weight: bold;'>✓ Shipped</span>">
            </if>
            <if data-test=""
                data-template="<span style='color: orange; font-weight: bold;'>⧗ Pending</span>">
            </if>
            <if data-test=""
                data-template="<span style='color: red; font-weight: bold;'>✗ Cancelled</span>">
            </if>
        </td>
    </tr>
</table>

13. Internationalized Content

Load locale-specific templates:

<!-- Model: { language: "en", contentEn: "<p>English content</p>", contentEs: "<p>Contenido español</p>" } -->
<if data-test=""
    data-template="">
</if>

<if data-test=""
    data-template="">
</if>

14. Conditional QR Code

Show QR code with description:

<if data-test=""
    data-template="<div style='text-align: center; margin: 20pt 0;'>
                      <img src='' style='width: 100pt; height: 100pt;'/>
                      <div style='font-size: 8pt; color: #666; margin-top: 5pt;'>
                        Scan to access online version
                      </div>
                   </div>">
</if>

15. Terms and Conditions Section

Conditionally include terms:

<if data-test=""
    data-template="<div style='margin-top: 30pt; padding: 15pt; border-top: 2pt solid black;'>
                      <h3 style='margin: 0 0 10pt 0;'>Terms and Conditions</h3>
                      <div style='font-size: 8pt; line-height: 1.4;'>
                        
                      </div>
                   </div>">
</if>

16. Status Indicator Panel

Multi-line status panel:

<if data-test=""
    data-template="<div style='background-color: #fff3cd; border-left: 5pt solid #ffc107; padding: 15pt; margin: 10pt 0;'>
                      <div style='font-weight: bold; font-size: 12pt; margin-bottom: 5pt;'>
                        System Status Alert
                      </div>
                      <div>
                        Current Status: <strong></strong>
                      </div>
                      <div style='margin-top: 5pt; font-size: 9pt; color: #666;'>
                        Last Updated: 
                      </div>
                   </div>">
</if>

17. Conditional Signature Block

Add signature block for certain document types:

<if data-test=""
    data-template="<div style='margin-top: 40pt; padding: 20pt; border: 1pt solid #000;'>
                      <div style='margin-bottom: 30pt;'>
                        <div>Signature: _________________________________</div>
                      </div>
                      <div style='margin-bottom: 10pt;'>
                        <div>Print Name: _________________________________</div>
                      </div>
                      <div>
                        <div>Date: _________________________________</div>
                      </div>
                   </div>">
</if>

18. Simple Payment Instructions

Conditional payment details:

<if data-test=""
    data-template="<div style='background-color: #d1ecf1; padding: 10pt; margin: 10pt 0;'>
                      <strong>Payment Instructions:</strong><br/>
                      Please make check payable to: <br/>
                      Mail to: 
                   </div>">
</if>

<if data-test=""
    data-template="<div style='background-color: #d1ecf1; padding: 10pt; margin: 10pt 0;'>
                      <strong>Wire Transfer Instructions:</strong><br/>
                      Bank: <br/>
                      Account: <br/>
                      Routing: 
                   </div>">
</if>

19. Promotional Banner

Conditional promotional content:

<if data-test=""
    data-template="<div style='background: linear-gradient(to right, #667eea, #764ba2);
                               color: white; padding: 20pt; text-align: center;
                               margin-bottom: 20pt; border-radius: 5pt;'>
                      <div style='font-size: 18pt; font-weight: bold; margin-bottom: 5pt;'>
                        
                      </div>
                      <div style='font-size: 12pt;'>
                        
                      </div>
                      <div style='margin-top: 10pt; font-size: 9pt;'>
                        Offer expires: 
                      </div>
                   </div>">
</if>

20. Complex Conditional Layout

Multi-section conditional content:

<if data-test=""
    data-template="<div style='border: 2pt solid #336699; padding: 15pt; margin: 20pt 0;'>
                      <div style='background-color: #336699; color: white; padding: 10pt; margin: -15pt -15pt 15pt -15pt;'>
                        <h2 style='margin: 0;'>Detailed Summary</h2>
                      </div>
                      <table style='width: 100%; border-collapse: collapse;'>
                        <tr>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; font-weight: bold;'>Total Items:</td>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; text-align: right;'></td>
                        </tr>
                        <tr>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; font-weight: bold;'>Subtotal:</td>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; text-align: right;'>$</td>
                        </tr>
                        <tr>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; font-weight: bold;'>Tax:</td>
                          <td style='padding: 5pt; border-bottom: 1pt solid #ddd; text-align: right;'>$</td>
                        </tr>
                        <tr>
                          <td style='padding: 5pt; font-weight: bold; font-size: 12pt;'>Total:</td>
                          <td style='padding: 5pt; text-align: right; font-weight: bold; font-size: 12pt;'>$</td>
                        </tr>
                      </table>
                   </div>">
</if>

See Also