{% macro ko_grid(grid_options, template_args=None, wrapper_dom_attrs=None, template_dom_attrs=None) -%}
{% if template_args is none -%}
    {% set template_args = {} -%}
{% endif -%}
{% if wrapper_dom_attrs is none -%}
    {% set wrapper_dom_attrs = {} -%}
{% endif -%}
{% if template_dom_attrs is none -%}
    {% set template_dom_attrs = {} -%}
{% endif -%}
{#

Setup underscore.js template args (stored to template_dom_attrs as json at later step).

-#}
{%- set _template_args = {
    'show_title': True,
    'show_pagination': True,
    'vscroll': False
} -%}
{% do _template_args.update(template_args) -%}
{#

Setup url name kwargs both for tpl.resolve_cbv() and as grid_options.pageRoute / .pageRouteKwargs

-#}
{% set view_kwargs = {} -%}
{% if grid_options.pageRouteKwargsKeys -%}
    {% for key in grid_options.pageRouteKwargsKeys -%}
        {% if key in request.view_kwargs -%}
            {% do view_kwargs.update({key: request.view_kwargs[key]}) -%}
        {% endif -%}
    {% endfor -%}
{% endif -%}
{% if grid_options.pageRouteKwargs -%}
    {% do view_kwargs.update(grid_options.pageRouteKwargs) -%}
{% endif -%}
{% if view_kwargs|length > 0 -%}
    {% do sdv.nested_update(grid_options, {'pageRouteKwargs': view_kwargs}) -%}
{% endif -%}
{% do view_kwargs.update({'action': ''}) -%}
{% set view = tpl.resolve_cbv(grid_options.pageRoute, view_kwargs) -%}
{#

Setup final _grid_options (component options).

-#}
{% set _grid_options = view.discover_grid_options(request) -%}
{% do _grid_options.update(grid_options) -%}
{#

Setup wrapper dom_attrs.

-#}
{% do tpl.add_css_classes_to_dict(wrapper_dom_attrs, 'component') -%}
{% if 'classPath' in _grid_options -%}
    {% do wrapper_dom_attrs.update({'data-component-class': _grid_options['classPath']}) -%}
    {% do _grid_options.pop('classPath') -%}
{% else -%}
    {% do wrapper_dom_attrs.update({'data-component-class': 'App.ko.Grid'}) -%}
{% endif -%}
{% if 'name' in wrapper_dom_attrs -%}
    {% set fragment_name = wrapper_dom_attrs['name'] -%}
    {% do wrapper_dom_attrs.pop('name') -%}
{% elif 'id' in wrapper_dom_attrs -%}
    {% set fragment_name = wrapper_dom_attrs['id'] -%}
{% endif -%}
{#

Setup template dom_attrs.

-#}
{% set _template_dom_attrs = {
    'data-template-id': 'ko_grid_body',
    'data-template-args': _template_args,
    'data-template-options': {'meta_is_grid': True},
} -%}
{% do sdv.nested_update(_template_dom_attrs, template_dom_attrs) -%}
{#

Optional caller support.

-#}
{% if caller -%}
{{ caller({
    'wrapper_dom_attrs': wrapper_dom_attrs,
    '_grid_options': _grid_options,
    'fragment_name': fragment_name,
    '_template_dom_attrs': _template_dom_attrs
}) }}
{% else -%}
<div{{ tpl.json_flatatt(wrapper_dom_attrs) }} data-component-options='{{ _grid_options|escapejs(True) }}'>
<a name="{{ fragment_name }}"></a>
    <div{{ tpl.json_flatatt(_template_dom_attrs) }}>
    </div>
</div>
{% endif -%}
{% endmacro -%}
{% if _render_ -%}
    {% if not template_args is defined -%}
        {% set template_args = {} -%}
    {% endif -%}
    {% if not wrapper_dom_attrs is defined -%}
        {% set wrapper_dom_attrs = {} -%}
    {% endif -%}
    {% if not template_dom_attrs is defined -%}
        {% set template_dom_attrs = {} -%}
    {% endif -%}
    {{ ko_grid(
        grid_options,
        template_args=template_args,
        wrapper_dom_attrs=wrapper_dom_attrs,
        template_dom_attrs=template_dom_attrs
    ) }}
{% endif -%}
