Tokens in Django Bindings

Django Bindings has been deprecated. For more, see the Deprecation Notice.

The Splunk Web Framework uses tokens to provide a data-binding mechanism so that search managers and views can share a data value and stay in sync. For example, let's say you want users to be able to select an index to search from a dropdown list, and then you want to run the search again each time a selection is made. The shared data value is the index―the index that is selected from the dropdown list, and the index that the search query uses. You can link these values using a token, which is like a variable but is specified using the escape character "$". For example, in both the dropdown list and the search query, use a token called $indexName$ to hold the index variable.

 

Token syntax

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$"), append "|token_safe" to indicate tokens:

value="$tokenName$"|token_safe

 

Examples in Django template tags

This example shows how to set up a dropdown list and search manager with an $indexName$ token in Django template tags:

{% dropdown
    id="selIndex"
    value="$indexName$"|token_safe %}

...

{% searchmanager
    id="mySearch"
    search="index=$indexName$"|token_safe %}

This example shows different ways to display the value of the token on the page using HTML syntax and Django template syntax:

{% block content %}

    ...

    Display the text using HTML syntax:<br/>
    - Using a span tag: <span class="splunk-tokensafe" data-value="$indexName$"></span><br/>

    Display the text in Django template syntax:<br/>
    - As a Django variable: {{ "$indexName$"|token_safe }}<br/>
    - As a filter tag: {% filter token_safe %}$indexName${% endfilter %}<br/>

{% endblock content%}

You can assign multiple tokens to a property value. This example builds on the previous example by adding a text box that accepts search keywords. The search query now uses two token variables―one for the index name and one for keywords:

{% dropdown
    id="selIndex"
    value="$indexName$"|token_safe %}

{% textinput
    id="txtKeywords"
    value="$keywords$"|token_safe %}

...

{% searchmanager
    id="mySearch"
    search="index=$indexName$ $keywords$"|token_safe %}

    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.

 

Default token values

Some Splunk views can take a default value to define the initial value of the view. If the view's value is a token, the token will be initially set to the default value. In this example, the initial value for both the text box and dropdown list is "Top five sourcetypes":

{% textinput
    id="txtSavedSearch"
    value="$savedSearch$"|token_safe
    default="Top five sourcetypes" %}

{% dropdown
    id="selSavedSearch"
    value="$savedSearch$"|token_safe %}

If multiple views are linked to the same token, and multiple default values are specified, the last value that is set is the one that is used. In this example, the initial value for both views is "Errors in the last hour":

{% textinput
    id="txtSavedSearch"
    value="$savedSearch$"|token_safe
    default="Top five sourcetypes" %}

{% dropdown
    id="selSavedSearch"
    value="$savedSearch$"|token_safe
    default="Errors in the last hour" %}

 

For more information

For more about how tokens work in SplunkJS Stack, see Bind data using tokens.

For more about using tokens in Simple XML dashboards, see Token usage in dashboards in the Dashboards and Visualizations manual.