---
metadata:
  - name: generator
    content: Diplodoc Platform v5.39.1
alternate:
  - https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/examples/cases/button_layout.md
  - https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/examples/cases/button_layout.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.ru/dev/jsapi-v2-1/doc/ru/llms.txt

# Произвольный макет кнопки

<iframe id="LIVE-EXAMPLE" style="width:100%;height: 400px;border-radius: 8px;border: 1px solid rgba(92, 94, 102, 0.14);" frameBorder="0" src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-v2-1/examples/2/out/s3-cases/ru/button_layout/index.html" allow="fullscreen"></iframe>

<a target="_blank" rel="noopener noreferrer" style="display: inline-block;margin-top: 10px;cursor: pointer;border-radius: 4px;padding: 9px 12px;background: #151515;color: white;text-decoration: none;font-weight: 500" href="https://codesandbox.io/p/sandbox/cnp9cz?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/ready">ready</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/control.Button">control.Button</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a>
</div>
<p>
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/dg/concepts/layouts">Макеты</a>
    объектов можно создавать с помощью фабрики <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a>,
    используя текстовые шаблоны.
</p>
<p>
    В данном примере создается пользовательский макет <a href="https://yandex.ru/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/control.Button">кнопки</a>.
    Макет элемента управления строится на основе его данных, состояния и опций.
</p>
<p>
    Макет автоматически перестраивается при изменении значений полей,
    состояния или опций, которые используются в его текстовом шаблоне.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>

    <html>
        <head>
            <title>Произвольный макет кнопки</title>
            <meta
                http-equiv="Content-Type"
                content="text/html; charset=utf-8"
            />
            <!--
            Укажите свой API-ключ. Тестовый ключ НЕ БУДЕТ работать на других сайтах.
            Получить ключ можно в Кабинете разработчика: https://developer.tech.yandex.ru/keys/
        -->
            
            
            
        </head>

        <body>
            <div id="map"></div>
        </body>
    </html>
    ```

-   button_layout.js

    ```js
    ymaps.ready(init);

    function init() {
        var myMap = new ymaps.Map("map", {
                center: [55.650625, 37.62708],
                zoom: 10,
                controls: [],
            }),
            /*
             * Макет кнопки должен отображать поле data.content
             * и изменяться в зависимости от того, нажата ли кнопка или нет.
             * Текущий размер (small, medium, large) рассчитывается исходя из значения опции maxWidth
             * @see https://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/control.Button.xml#param-parameters
             */
            ButtonLayout = ymaps.templateLayoutFactory.createClass(
                [
                    '<div title="{{ data.title }}" class="my-button ',
                    '{% if state.size == "small" %}my-button_small{% endif %}',
                    '{% if state.size == "medium" %}my-button_medium{% endif %}',
                    '{% if state.size == "large" %}my-button_large{% endif %}',
                    '{% if state.selected %} my-button-selected{% endif %}">',
                    '<img class="my-button__img" src="{{ data.image }}" alt="{{ data.title }}">',
                    '<span class="my-button__text">{{ data.content }}</span>',
                    "</div>",
                ].join("")
            ),
            button = new ymaps.control.Button({
                data: {
                    content: "Жмак-жмак-жмак",
                    image: "pen.svg",
                    title: "Жмак-жмак-жмак",
                },
                options: {
                    layout: ButtonLayout,
                    maxWidth: [170, 190, 220],
                },
            });

        myMap.controls.add(button, {
            position: {
                right: 5,
                top: 5,
            },
        });
    }
    ```

{% endlist %}
