Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Netcarver/Textile/Tag.php - 118 lines - 3357 bytes - Summary - Text - Print

Description: Textile - A Humane Web Text Generator.

   1  <?php
   2  
   3  /**
   4   * Textile - A Humane Web Text Generator.
   5   *
   6   * @link https://github.com/textile/php-textile
   7   */
   8  
   9  namespace Netcarver\Textile;
  10  
  11  /*
  12   * Copyright (c) 2013, Netcarver https://github.com/netcarver
  13   *
  14   * Redistribution and use in source and binary forms, with or without
  15   * modification, are permitted provided that the following conditions are met:
  16   *
  17   * * Redistributions of source code must retain the above copyright notice,
  18   * this list of conditions and the following disclaimer.
  19   *
  20   * * Redistributions in binary form must reproduce the above copyright notice,
  21   * this list of conditions and the following disclaimer in the documentation
  22   * and/or other materials provided with the distribution.
  23   *
  24   * * Neither the name Textile nor the names of its contributors may be used to
  25   * endorse or promote products derived from this software without specific
  26   * prior written permission.
  27   *
  28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38   * POSSIBILITY OF SUCH DAMAGE.
  39   */
  40  
  41  /**
  42   * Renders HTML elements.
  43   *
  44   * This class can be used to HTML elements. It
  45   * does not sanitise attribute values, but can be
  46   * used to construct tags with nice object oriented
  47   * syntax.
  48   *
  49   * <code>
  50   * use Netcarver\Textile\Tag;
  51   * $img = new Tag('img');
  52   * echo (string) $img->class('big blue')->src('images/elephant.jpg');
  53   * </code>
  54   */
  55  
  56  class Tag extends DataBag
  57  {
  58      /**
  59       * The name of the tag.
  60       *
  61       * @var string
  62       */
  63  
  64      protected $tag;
  65  
  66      /**
  67       * Whether the tag is self-closing.
  68       *
  69       * @var bool
  70       */
  71  
  72      protected $selfclose;
  73  
  74      /**
  75       * Constructor.
  76       *
  77       * @param string $name        The tag name
  78       * @param array  $attributes  An array of attributes
  79       * @param bool   $selfclosing Whether the tag is self-closing
  80       */
  81  
  82      public function __construct($name, array $attributes = null, $selfclosing = true)
  83      {
  84          parent::__construct($attributes);
  85          $this->tag = $name;
  86          $this->selfclose = $selfclosing;
  87      }
  88  
  89      /**
  90       * Returns the tag as HTML.
  91       *
  92       * <code>
  93       * $img = new Tag('img');
  94       * $img->src('images/example.jpg')->alt('Example image');
  95       * echo (string) $img;
  96       * </code>
  97       *
  98       * @return string A HTML element
  99       */
 100  
 101      public function __toString()
 102      {
 103          $attributes = '';
 104  
 105          if ($this->data) {
 106              ksort($this->data);
 107              foreach ($this->data as $name => $value) {
 108                  $attributes .= " $name=\"$value\"";
 109              }
 110          }
 111  
 112          if ($this->tag) {
 113              return '<' . $this->tag . $attributes . (($this->selfclose) ? " />" : '>');
 114          }
 115  
 116          return $attributes;
 117      }
 118  }

title

Description

title

Description

title

Description

title

title

Body