示例#1
0
文件: Field.php 项目: atk4/atk4
 public function getTag($tag, $attr = null, $value = null)
 {
     /**
      * Draw HTML attribute with supplied attributes.
      *
      * Short description how this getTag may be used:
      *
      * Use get tag to build HTML tag.
      * echo getTag('img',array('src'=>'foo.gif','border'=>0);
      *
      * The unobvius advantage of this function is ability to merge
      * attribute arrays. For example, if you have function, which
      * must display img tag, you may add optional $attr argument
      * to this function.
      *
      * function drawImage($src,$attr=array()){
      *     echo getTag('img',array_merge(array('src'=>$src),$attr));
      * }
      *
      * so calling drawImage('foo.gif') will echo: <img src="foo.gif">
      *
      * The benefit from such a function shows up when you use 2nd argument:
      *
      * 1. adding additional attributes
      * drawImage('foo.gif',array('border'=>0'));
      * --> <img src="foo.gif" border="0">
      * (NOTE: you can even have attr templates!)
      *
      * 2. adding no-value attributes, such as nowrap:
      * getTag('td',arary('nowrap'=>true));
      * --> <td nowrap>
      *
      * 3. disabling some attributes.
      * drawImage('foo.gif',array('src'=>false));
      * --> <img>
      *
      * 4. re-defining attributes
      * drawImage('foo.gif',array('src'=>'123'));
      * --> <img src="123">
      *
      * 5. or you even can re-define tag itself
      * drawImage('foo.gif',array(
      *                      ''=>'input',
      *                      'type'=>'picture'));
      * --> <input type="picture" src="foo.gif">
      *
      * 6. xml-valid tags without closing tag
      * getTag('img/',array('src'=>'foo.gif'));
      * --> <img src=>"foo.gif"/>
      *
      * 7. closing tags
      * getTag('/td');
      * --> </td>
      *
      * 8. using $value will add $value after tag followed by closing tag
      * getTag('a',array('href'=>'foo.html'),'click here');
      * --> <a href="foo.html">click here</a>
      *
      * 9. you may not skip attribute argument.
      * getTag('b','text in bold');
      * --> <b>text in bold</b>
      *
      * 10. nesting
      * getTag('a',array('href'=>'foo.html'),getTag('b','click here'));
      * --> <a href="foo.html"><b>click here</b></a>
      */
     if (is_string($attr)) {
         $value = $attr;
         $attr = null;
     }
     if (!$attr) {
         return "<{$tag}>" . ($value ? $value . "</{$tag}>" : '');
     }
     $tmp = array();
     if (substr($tag, -1) == '/') {
         $tag = substr($tag, 0, -1);
         $postfix = '/';
     } else {
         $postfix = '';
     }
     foreach ($attr as $key => $val) {
         if ($val === false) {
             continue;
         }
         if ($val === true) {
             $tmp[] = "{$key}";
         } elseif ($key === '') {
             $tag = $val;
         } else {
             $tmp[] = "{$key}=\"" . $this->app->encodeHtmlChars($val) . '"';
         }
     }
     return "<{$tag} " . implode(' ', $tmp) . $postfix . '>' . ($value ? $value . "</{$tag}>" : '');
 }