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

# Custom button layout

<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/en/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/mwgws6?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ready">ready</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/control.Button">control.Button</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a>
</div>
<p>
    Object <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/dg/concepts/layouts">layouts</a>
    can be created using the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a> factory
    and text templates.
</p>
<p>
    This example creates a custom layout for a <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/control.Button">button</a>.
    The layout of the control is based on its data, status, and options.
</p>
<p>
    The layout is automatically adjusted when changes are made to the values
	of fields, states, or options that are used in its text template.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>

    <html>
        <head>
            <title>Examples. Custom layout for a button.</title>
            <meta
                http-equiv="Content-Type"
                content="text/html; charset=UTF-8"
            />
            <!--
            Set your own API-key. Testing key is not valid for other web-sites and services.
            Get your API-key on the Developer Dashboard: 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: [],
            }),
            /**
             * The button layout should display the 'data.content' field
             * and change depending on whether the button was clicked or not.
             * The current size (small, medium, large) is calculated from the value of the 'maxWidth' option
             * @see https://api.yandex.com/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: "Click-click-click",
                    image: "pen.svg",
                    title: "Click-click-click",
                },
                options: {
                    layout: ButtonLayout,
                    maxWidth: [170, 190, 220],
                },
            });

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

{% endlist %}
