Token syntax

To use tokens in a Simple XML extension, make sure to require the "splunkjs/mvc" library:

require([
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function(mvc) {

    ...

});

To indicate a token, use the escape character "$" on either side of the token name, for example $tokenName$. To indicate that a variable is set to a token and not to a literal string that contains dollar signs (for example, the literal "$1.00" versus the token "$myToken$"), use the mvc.tokenSafe syntax:

value: mvc.tokenSafe("$tokenName$")

Examples

This example shows how to set up a dropdown list that uses an index name token, a text box that uses a keywords token, and a search manager that uses multiple token variables—one for the index name and one for keywords:

new DropdownView({
    id: "selIndex",
    value: mvc.tokenSafe("$indexName$"),
    el: $("#selIndex")
}).render();

new TextInputView({
    id: "txtKeywords",
    value: mvc.tokenSafe("$keywords$"),
    el: $("#txtKeywords")
}).render();

new SearchManager({
    id: "mySearch",
    search: mvc.tokenSafe("index=$indexName$ $keywords$")
});

    A tip for debugging  When a property has one or more tokens, that property is undefined until all of its tokens take a value. In the example above, the search does not run until both $indexName$ and $keywords$ are set.

By using tokens in this example, whenever the user selects a different index or enters different keywords, the search query is updated with these values, a new search is run, and the new results are displayed automatically. That's it―you don't need to write any more code to link these values and keep them in sync.

You can also apply a token filter to all properties of a view or search manager at once (rather than to each individual property) using the {tokens: true} option. The following example shows how to instantiate a search manager using both methods.

// Mark individual values in the search manager as token safe
new SearchManager({
    id: "search1",
    earliest_time: mvc.tokenSafe("$earliestTime$"),
    latest_time: mvc.tokenSafe("$latestTime$"),
    search: mvc.tokenSafe("index=$indexName$ | head 50")
});

// Mark all values in the search manager as token safe
new SearchManager({
    id: "search2",
    earliest_time: "$earliestTime$",
    latest_time: "$latestTime$",
    search: "index=$indexName$ | head 50"
}, {tokens: true});

You can also mark individual properties as literal strings using a filter. Let's say you want to mark all values but one as tokens. Use {tokens: true} to mark all values in the constructor as tokens, and then use the mvc.tokenEscape filter to mark an individual value as a literal string. For example:

// Use {tokens: true} to mark all values in this search manager as token safe
new SearchManager({
    id: "search3",
    earliest_time: "$earliestTime$",
    latest_time: "$latestTime$",
    search: mvc.tokenEscape("search price=$1.00") // Literal string value
}, {tokens: true});