template.filtersStorage

Static object.

Instance of util.Storage

Storage for template filters. Filters from storage can be used in all layouts created using templateLayoutFactory. By default, the storage declares the following keys for filters:

  • default — Allows setting default values. For example, like this: {{ properties.header|default:"Title" }}

Methods

Examples:

1.

// Writing a simple filter that will convert a date
// from the format "dd.mm.yyyy" to the format "dd month yyyy".
// To do this, we need to create a filter function that will return the new value.

// When the filter is called, the following arguments are passed to the filter:
// the template data manager data.Manager, the value, and the value set for the filter.
var dateFilter = function (data, dateString, filterValue) {
    var months = [
        'january', 'february', 'march', 'april',
        'may', 'june', 'july', 'august',
        'september', 'october', 'november', 'december'
    ];
    var date = dateString.split('.');

    date[1] = months[parseInt(date[1], 10)];
    return date.join(' ');
};

ymaps.template.filtersStorage.add('date', dateFilter);

// Then we can use it in constructions like
// {{ "21.10.2014"|date }} the value will be "21 october 2014".

2.

// Writing a filter that will find and replace substrings in text.
// The format for substitution values in the filter is "subString_newSubString".

// When the filter is called, the following arguments are passed to the function:
// the template data manager data.Manager, the text, and the value set for the filter.
var replaceFilter = function (data, text, replace) {
    replace = replace.trim();
    // Removing quotation marks.
    replace = replace.slice(1, replace.length - 1);

    // Finding the part that comes before "_" in the text and replacing it with what comes after it.
    var values = replace.split('_');
    var from = new RegExp(values[0], 'g');
    var to = values[1];

    return text.replace(from, to);
};

// Now we can use this in templates of constructions like
// {{"text test replace"|replace: "test_replaced test" }} the value will be "text replaced test replace".

3.

// In this example, the value of the "colorClass" option and the value of the "header" property are added to the layout. 
// If the "header" property doesn't have a value, the string "Title" is inserted.
var LayoutClass = ymaps.templateLayoutFactory.createClass(
    '<h1 class="{{ options.colorClass }}">' +
    '{{ properties.header|default:"Title" }}' +
    '</h1>'
);

Methods

Name

Returns

Description

add(key, object)

util.Storage

Adds an object to storage.

get(key)

Object

Returns object stored for the specified key, or the primary key, if this is not a string.

remove(key)

util.Storage

Deletes the "key: value" pair from storage.