'Helper function for outputting HTML. Used by as_table(), as_ul(), as_p().'

def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):

    top_errors = self.non_field_errors()
    (output, hidden_fields) = ([], [])
    for (name, field) in self.fields.items():
        html_class_attr = u''
        bf = self[name]
        bf_errors = self.error_class([conditional_escape(error) for error in bf.errors])
        if bf.is_hidden:
            if bf_errors:
                top_errors.extend([(_(u'(Hidden    field    %(name)s)    %(error)s') % {u'name': name, u'error': force_text(e)}) for e in bf_errors])
            hidden_fields.append(six.text_type(bf))
        else:
            css_classes = bf.css_classes()
            if css_classes:
                html_class_attr = (u'    class="%s"' % css_classes)
            if (errors_on_separate_row and bf_errors):
                output.append((error_row % force_text(bf_errors)))
            if bf.label:
                label = conditional_escape(force_text(bf.label))
                label = (bf.label_tag(label) or u'')
            else:
                label = u''
            if field.help_text:
                help_text = (help_text_html % force_text(field.help_text))
            else:
                help_text = u''
            output.append((normal_row % {u'errors': force_text(bf_errors), u'label': force_text(label), u'field': six.text_type(bf), u'help_text': help_text, u'html_class_attr': html_class_attr}))
    if top_errors:
        output.insert(0, (error_row % force_text(top_errors)))
    if hidden_fields:
        str_hidden = u''.join(hidden_fields)
        if output:
            last_row = output[(-1)]
            if (not last_row.endswith(row_ender)):
                last_row = (normal_row % {u'errors': u'', u'label': u'', u'field': u'', u'help_text': u'', u'html_class_attr': html_class_attr})
                output.append(last_row)
            output[(-1)] = ((last_row[:(- len(row_ender))] + str_hidden) + row_ender)
        else:
            output.append(str_hidden)
    return mark_safe(u'\n'.join(output))
