共用方式為


調適型卡片範本化 SDK

調適型卡片範本化 SDK 可讓您輕鬆地在任何支援的平臺上填入具有實際數據的 卡片範本

如需調適型卡片範本化的概觀,請閱讀這篇文章

這很重要

2020 年 5 月候選版的重大變更

我們一直在努力準備範本功能的釋出,現在已經到了最關鍵的階段! 我們必須在接近發佈時進行一些對功能造成影響的小變更。

截至 2020 年 5 月的重大變更

  1. 系結語法已從 {...} 變更為 ${...}
    • 例如: "text": "Hello {name}" 變成 "text": "Hello ${name}"
  2. JavaScript API 不再包含 EvaluationContext 物件。 只要將數據傳遞至函式 expand 即可。 如需完整詳細數據,請參閱 SDK 頁面
  3. .NET API 經過重新設計,以更緊密地符合 JavaScript API。 如需完整詳細數據,請參閱下方。

JavaScript

adaptivecards-templating 連結庫可在 npm 和透過 CDN 取得。 如需完整檔,請參閱套件連結。

npm

npm install

npm install adaptivecards-templating

CDN

<script src="https://unpkg.com/adaptivecards-templating/dist/adaptivecards-templating.min.js"></script>

用法

下列範例假設您也已安裝 adaptivecards 函式庫,以便渲染卡片。

如果您不打算轉譯卡片,則可以移除 parserender 程序代碼。

import * as ACData from "adaptivecards-templating";
import * as AdaptiveCards from "adaptivecards";
 
// Define a template payload
var templatePayload = {
    "type": "AdaptiveCard",
    "version": "1.0",
    "body": [
        {
            "type": "TextBlock",
            "text": "Hello ${name}!"
        }
    ]
};
 
// Create a Template instance from the template payload
var template = new ACData.Template(templatePayload);
 
// Expand the template with your `$root` data object.
// This binds it to the data and produces the final Adaptive Card payload
var cardPayload = template.expand({
   $root: {
      name: "Matt Hidinger"
   }
});
 
// OPTIONAL: Render the card (requires that the adaptivecards library be loaded)
var adaptiveCard = new AdaptiveCards.AdaptiveCard();
adaptiveCard.parse(cardPayload);
document.getElementById('exampleDiv').appendChild(adaptiveCard.render());

.NET

NuGet install

dotnet add package AdaptiveCards.Templating

用法

// Import the library 
using AdaptiveCards.Templating;
var templateJson = @"
{
    ""type"": ""AdaptiveCard"",
    ""version"": ""1.2"",
    ""body"": [
        {
            ""type"": ""TextBlock"",
            ""text"": ""Hello ${name}!""
        }
    ]
}";

// Create a Template instance from the template payload
AdaptiveCardTemplate template = new AdaptiveCardTemplate(templateJson);

// You can use any serializable object as your data
var myData = new
{
    Name = "Matt Hidinger"
};

// "Expand" the template - this generates the final Adaptive Card payload
string cardJson = template.Expand(myData);

自訂函式

除了預先建置的函式之外,還可以將自定義函式新增至自適性表達式連結庫。

在下列範例中,會新增 stringFormat 自定義函式,並使用運算來格式化字串。

string jsonTemplate = @"{
    ""type"": ""AdaptiveCard"",
    ""version"": ""1.0"",
    ""body"": [{
        ""type"": ""TextBlock"",
        ""text"": ""${stringFormat(strings.myName, person.firstName, person.lastName)}""
    }]
}";

string jsonData = @"{
    ""strings"": {
        ""myName"": ""My name is {0} {1}""
    },
    ""person"": {
        ""firstName"": ""Andrew"",
        ""lastName"": ""Leader""
    }
}";

AdaptiveCardTemplate template = new AdaptiveCardTemplate(jsonTemplate);

var context = new EvaluationContext
{
    Root = jsonData
};

// a custom function is added
AdaptiveExpressions.Expression.Functions.Add("stringFormat", (args) =>
{
    string formattedString = "";

    // argument is packed in sequential order as defined in the template
    // For example, suppose we have "${stringFormat(strings.myName, person.firstName, person.lastName)}"
    // args will have following entries
    // args[0]: strings.myName
    // args[1]: person.firstName
    // args[2]: strings.lastName
    if (args[0] != null && args[1] != null && args[2] != null) 
    {
        string formatString = args[0];
        string[] stringArguments = {args[1], args[2] };
        formattedString = string.Format(formatString, stringArguments);
    }
    return formattedString;
});

string cardJson = template.Expand(context);

故障排除

問: 為什麼我會遇到 AdaptiveTemplateException 當呼叫 expand() 時?
A。 如果您的錯誤訊息看起來像是「<有問題的項目>」,是在第「<行號>」的『$data : 配對』格式不正確。
請確保為「$data」提供的值是有效的 JSON,例如數字 (number)、布林值 (boolean)、物件 (object)、和陣列 (array),或遵循調適型範本語言的正確語法,且項目存在於行號對應的數據內容中。

問: 為什麼在呼叫 expand() 時會遇到 ArgumentNullException?
A。 如果您的錯誤訊息看起來像「檢查父數據內容是否已設定,或者請輸入非 Null 值給『違規專案>』於行『<<行號>』」。
它顯示要求的資料綁定沒有對應的資料內容。 請確定已設定根數據內容,如果有的話,請確定任何數據內容都可供目前的系結使用,如行號所指示。

問: 為什麼 RFC 3389 格式的日期/時間,例如「2017-02-14T06:08:00Z」 搭配範本使用時,不適用於 TIME/DATE 函式?
A。 .NET sdk nuget 1.0.0-rc.0 版會展示此行為。 後續版本中會更正此行為。 json.Net 反序列化器的預設行為會修改日期/時間格式字串,未來版本將停用此行為。 請使用 formatDateTime() 函式,將日期/時間字元串格式化為 RFC 3389,如 此範例所示,或者您可以略過 TIME/DATE 函式,並只使用 formatDateTime()。 如需 formatDateTime() 的詳細資訊,請 前往這裡