2017 © Pedro PelĆ”ez
 

library glaze

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

image

burntcaramel/glaze

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  • Tuesday, May 5, 2015
  • by BurntCaramel
  • Repository
  • 2 Watchers
  • 8 Stars
  • 52 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Glaze

French and Viennese pastry chefs originally invented the idea of glazing cakes as a way of preserving them—the glaze sealed off the cakes from the air and prevented them from growing stale.

When displaying anything on the web it must be properly prepared for HTML. Any text, any URL, and any HTML element’s attributes and contents must be escaped before they are displayed., (*1)

Normally people use functions like htmlspecialchars(), or they even madly paste text into their source code and manually change characters like & into & and > into >., (*2)

Well there’s these things called computers and you can avoid all that manual work and use much more powerful functions with baking-inspired names., (*3)

Glaze preserves the content you want to display.

Just tell it what you want to display and let it worry about the HTML writing and escaping part. It works with nested HTML elements, attributes, text, URLs, and email addresses. My aim is to make it easier to read and write than the usual PHP ways, whilst also taking care of escaping everything by default., (*4)

Whole elements

Escaped elements in one line., (*5)

use BurntCaramel\Glaze;
use BurntCaramel\Glaze\Prepare as GlazePrepare;
use BurntCaramel\Glaze\Serve as GlazeServe;

GlazeServe::element('h1#siteTitle', 'Title');
// No need to escape the &
GlazeServe::element('h2.tagline', 'The home of examples & more');
GlazeServe::element('p.any.classes.you.need', 'Blah blah blah blah');

/* Displays: */?>


Title

The home of examples & more

Blah blah blah blah , (*6)

Or use associated array version, to specify any attributes you like:, (*7)

GlazeServe::element(array(
    'tagName' => 'a',
    'href' => 'http://www.infinitylist.com/',
    'class' => 'externalLink'
), 'Adventure & creative videos.');

/* Displays: */?>
<a href="http://www.infinitylist.com/" class="externalLink">Adventure &amp; creative videos.</a>

Self closing elements are also handled., (*8)

GlazeServe::element(array(
    'tagName' => 'meta',
    'name' => 'description',
    'content' => 'Site description as seen by search engines'
));

/* Displays: */?>
<meta name="description" content="Site description as seen by search engines">

Class attributes

Say you want to display an HTML element’s class names where some are optional., (*9)

$classNames = array('post');

if (isArticle()):
    $classNames[] = 'article';

    if (isFeatureArticle()):
        $classNames[] = 'feature';
    endif;
endif;

You could juggle if statements and PHP open/close tags:, (*10)

?>
<div class="= implode(' ', $classNames); ?>">




<

div class="post article feature">

Or use Glaze, passing a string or array:, (*11)

// The -Checking method makes sure that if `$classNames` is empty, then nothing will be displayed.
?>
<div>




<

div class="post article feature">

Check values for attributes before displaying

Using JSON from a web API, for example., (*12)

$info = array(
    'itemID' => 'fddf3tq3tt3t3',
    'published' => false,
    'genreIdentifier' => 'thriller',
    'salesCount' => 56,
    'selected' => true,
    'authorName' => 'John Smith',
    'itemDescription' => array(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
        'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
        'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'
    )
);

// Build a list of classes using an array, don't fuss with appending to a string
$classNamesArray = array('item', 'book');
$classNamesArray[] = !empty($info['published']) ? 'published' : 'upcoming';
$classNamesArray[] = 'genre-' .$info['genreIdentifier']; // e.g. 'genre-thriller'

$bookItemDiv = GlazePrepare::element('div');
{
    $bookItemDiv->setAttribute('id', "bookItem-{$info['itemID']}");

    // Lets you use an array of strings for class attributes.
    $bookItemDiv->addClassNames($classNamesArray);

    // Only display the attribute if variable reference $info['salesCount'] is present.
    $bookItemDiv->setAttributeChecking('data-sales-count', $info['salesCount']);
    $bookItemDiv->setAttributeChecking('data-sales-count-nope', $info['salesCount_NOPE']);

    // Only displays the attribute, with the value 'selected', if $info['selected'] is true.
    $bookItemDiv->setAttributeChecking('selected', $info['selected'], 'selected');
    $bookItemDiv->setAttributeChecking('selected-nope', $info['selected_NOPE'], 'selected');

    // Will display:
    $bookItemDiv->appendNewElement('h5.authorName',
        Glaze\check($info['authorName'])
    );
    // Will not display, as key 'authorName_NOPE' does not exist.
    $bookItemDiv->appendNewElement('h5.authorName',
        Glaze\check($info['authorName_NOPE'])
    );

    // Will display:
    $bookItemDiv->appendNewElement('p.description',
        GlazePrepare::contentSeparatedBySoftLineBreaks(
            Glaze\check($info['itemDescription'])
        )
    );
    // Will not display, as key 'itemDescription_NOPE' does not exist.
    $bookItemDiv->appendNewElement('p.description',
        GlazePrepare::contentSeparatedBySoftLineBreaks(
            Glaze\check($info['itemDescription_NOPE'])
        )
    );
}
$bookItemDiv->serve();

Displays:, (*13)

<div id="bookItem-fddf3tq3tt3t3" class="item book upcoming genre-thriller" data-sales-count="56" selected="selected">
<h5 class="authorName">
John Smith
</h5>
<p class="description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<br>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</p>
</div>

Using already escaped information

$escapedText = 'Bangers &amp; Mash';
GlazeServe::attribute('alt', $escapedText, Glaze\TYPE_PREGLAZED);

/* Displays: */?>
 alt="Bangers &amp; Mash"

The Versions

05/05 2015

dev-master

9999999-dev

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze

15/04 2015

2.0.0-beta7

2.0.0.0-beta7

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze

28/03 2015

2.0.0-beta4

2.0.0.0-beta4

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze

24/03 2015

2.0.0-beta3

2.0.0.0-beta3

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze

21/03 2015

2.0.0-beta2

2.0.0.0-beta2

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

17/03 2015

1.9.3

1.9.3.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

01/02 2015

1.9.2

1.9.2.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

28/11 2014

1.9.1

1.9.1.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

12/11 2014

1.9.0

1.9.0.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

21/08 2014

1.8.0

1.8.0.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

21/07 2014

1.7.0

1.7.0.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

27/06 2014

1.6.5

1.6.5.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

27/06 2014

1.6.4

1.6.4.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

29/05 2014

1.6.3

1.6.3.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

27/05 2014

1.6.2

1.6.2.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

27/05 2014

1.6.1

1.6.1.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display glaze glazy

12/05 2014

1.6.0

1.6.0.0

Easy to use functions for displaying HTML attributes & elements, escaping text, URLs, email addresses.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

html text escape escaping display