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

# Custom cluster balloon 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/cluster_custom_balloon_content_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/mgdlsv?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/Clusterer">Clusterer</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ClusterPlacemark">ClusterPlacemark</a>, <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Placemark">Placemark</a>
</div>
    This example creates a custom layout for the cluster balloon content.<br/>
    The layout is specified with the 'clusterBalloonContentLayout' option.
<br/><a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/dg/concepts/layouts">Layouts</a>
    for objects can be created using the factory <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/templateLayoutFactory">templateLayoutFactory</a>
    and text templates. <br/>
    Text templates generate the HTML content of the layout from data hashes
    passed to the layout constructor.<br/>
    In this example, the data provider is the cluster where the balloon was opened.<br/><br/>
    The layout enumerates and outputs the 'placemarkId' and 'balloonContentHeader' fields for all the cluster's geo objects using the 'for' construction.

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <html>
            <head>
                <title>
                    Examples. Custom layout for cluster balloon content.
                </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>
    </html>
    ```

-   cluster_custom_balloon_content_layout.js

    ```js
    jQuery(function () {
        ymaps.ready(function () {
            var mapCenter = [55.755381, 37.619044],
                map = new ymaps.Map("map", {
                    center: mapCenter,
                    zoom: 10,
                    controls: [],
                }),
                placemarks = [];

            // Creating a custom layout with information about the selected geo object.
            var customBalloonContentLayout =
                ymaps.templateLayoutFactory.createClass(
                    [
                        "<ul class=list>",
                        // Outputting a list of all geo objects in the cycle.
                        "{% for geoObject in properties.geoObjects %}",
                        '<li><a href=# data-placemarkid="{{ geoObject.properties.placemarkId }}" class="list_item">{{ geoObject.properties.balloonContentHeader|raw }}</a></li>',
                        "{% endfor %}",
                        "</ul>",
                    ].join("")
                );

            jQuery(document).on("click", "a.list_item", function () {
                // Determining what placemark the event occurred on.
                var selectedPlacemark =
                    placemarks[jQuery(this).data().placemarkid];
                alert(selectedPlacemark.properties.get("balloonContentBody"));
            });

            var clusterer = new ymaps.Clusterer({
                clusterDisableClickZoom: true,
                clusterOpenBalloonOnClick: true,
                /**
                 * Setting the mode for opening the balloon.
                 * In this example, the balloon will never open in the panel mode.
                 */
                clusterBalloonPanelMaxMapArea: 0,
                /**
                 * By default, the balloon options balloonMaxWidth and balloonMaxHeight are not set for the clusterer,
                 * since all standard layouts have defined dimensions.
                 */
                clusterBalloonMaxHeight: 200,
                // Setting a custom layout for balloon content.
                clusterBalloonContentLayout: customBalloonContentLayout,
            });

            // Populating the cluster with geo objects with random positions.
            for (var i = 0, l = 100; i < l; i++) {
                var placemark = new ymaps.Placemark(getRandomPosition(), {
                    // Defining the data that will be displayed in the balloon.
                    balloonContentHeader:
                        "The title of the placemark #" + (i + 1),
                    balloonContentBody:
                        "Information about the placemark #" + (i + 1),
                    placemarkId: i,
                });
                placemarks.push(placemark);
            }

            clusterer.add(placemarks);
            map.geoObjects.add(clusterer);

            function getRandomPosition() {
                return [
                    mapCenter[0] + (Math.random() * 0.3 - 0.15),
                    mapCenter[1] + (Math.random() * 0.5 - 0.25),
                ];
            }
            clusterer.balloon.open(clusterer.getClusters()[0]);
        });
    });
    ```

{% endlist %}
