code: private void restoreTemplateScope(InternalContextAdapterImpl ica, Scope currentTemplateScope)
      {
          if (currentTemplateScope.getParent() != null) {
              ica.put(TEMPLATE_SCOPE_NAME, currentTemplateScope.getParent());
          } else if (currentTemplateScope.getReplaced() != null) {
              ica.put(TEMPLATE_SCOPE_NAME, currentTemplateScope.getReplaced());
          } else {
              ica.remove(TEMPLATE_SCOPE_NAME);
          }
      }
docs: Restore the previous {@code $template} variable, if any, in the velocity context.
  
  @param ica the current velocity context
  @param currentTemplateScope the current Scope, from which to take the replaced variable