Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-22155

Add output_nonempty_tag to html_writer

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0
    • 2.0
    • Libraries
    • None
    • MOODLE_20_STABLE
    • MOODLE_20_STABLE

    Description

      Petr, can I add this to html_writer?

          /**
           * Outputs a tag, but only if the contents are not empty
           * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
           * @param string $contents What goes between the opening and closing tags
           * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
           * @return string HTML fragment
           */
          public static function output_nonempty_tag($tagname, $contents, array $attributes = null) {
              if (empty($contents)) {
                  return '';
              }
              return self::output_tag($tagname, $contents, $attributes);
          }

      I have been using it in my new question code, and it simplifies a lot of renderer code like

          html_writer::output_nonempty_tag('div', $thing->get_feedback(), array('class' => 'feedback');

      The div only gets output if there is some feedback.

      Attachments

        Activity

          People

            timhunt Tim Hunt
            timhunt Tim Hunt
            Nobody Nobody
            Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              24/Nov/10