{% if not helpers.empty('OPNsense.unboundplus.domains.domain') %}
{% set forwardlocal = namespace(found=False) %}
{% set domain_opts = namespace(forward_tcp_upstream=False) %}
{%   for domain in helpers.toList('OPNsense.unboundplus.domains.domain', 'domain') %}
{%     if domain.enabled == '1' %}
{%         if not loop.previtem or loop.previtem.domain != domain.domain %}
{%             set domain_opts.forward_tcp_upstream = False %}
forward-zone:
  name: "{{ domain.domain }}"
{%           if domain.server.startswith('127.') or domain.server == '::1' %}
{%             set forwardlocal.found = True %}
{%           endif %}
{%           set domain_opts.forward_tcp_upstream = domain_opts.forward_tcp_upstream or domain.forward_tcp_upstream == '1' %}
{%         endif %}
  forward-addr: {{ domain.server }}
{%       if not loop.nextitem or loop.nextitem.domain != domain.domain %}
{%           if domain_opts.forward_tcp_upstream %}
  forward-tcp-upstream: yes
{%           endif %}
{%       endif %}
{%     endif %}
{%   endfor %}
{%   if forwardlocal.found %}
server:
  do-not-query-localhost: no
{%   endif %}
{% endif %}
