burntcaramel / glaze
用于显示HTML属性和元素的简单易用的函数,转义文本、URL和电子邮件地址。
2.0.0-beta7
2015-04-15 09:16 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-10-02 04:30:00 UTC
README
法国和维也纳糕点师最初发明了给蛋糕上糖釉的想法,作为一种保存蛋糕的方法——糖釉将蛋糕与空气隔绝,防止其变干。
在网页上显示任何内容之前,必须将其正确地准备成HTML。任何文本、任何URL以及任何HTML元素的属性和内容在显示之前都必须进行转义。
通常人们使用像htmlspecialchars()
这样的函数,甚至有些人疯狂地将文本粘贴到源代码中,手动将字符如&
改为&
,将>
改为>
。
当然,有这些被称为计算机的东西,你可以避免所有这些手动工作,并使用具有烘焙灵感命名的更强大的函数。
糖釉可以保存你想要显示的内容。
只需告诉它你想显示什么,然后让它担心HTML编写和转义部分。它可以处理嵌套的HTML元素、属性、文本、URL和电子邮件地址。我的目标是让它比常规的PHP方法更容易阅读和编写,同时默认转义所有内容。
完整元素
一行内转义的元素。
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: */?> <h1 id="siteTitle"> Title </h1> <h2 class="tagline"> The home of examples & more </h2> <p class="any classes you need"> Blah blah blah blah </p>
或者使用关联数组版本,指定任何你喜欢的属性
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 & creative videos.</a>
也可以处理自闭合元素。
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">
类属性
假设你想显示一个HTML元素的类名,其中一些是可选的。
$classNames = array('post'); if (isArticle()): $classNames[] = 'article'; if (isFeatureArticle()): $classNames[] = 'feature'; endif; endif;
你可以使用if
语句和PHP的开/闭标签来处理
?> <div<?php if (!empty($classNames)): ?> class="<?= implode(' ', $classNames); ?>"<?php endif; ?>> <?php/* Displays: */?> <div class="post article feature">
或者使用Glaze,传递一个字符串或数组
// The -Checking method makes sure that if `$classNames` is empty, then nothing will be displayed. ?> <div<?php GlazeServe::attributeChecking('class', $classNames); ?>> <?php/* Displays: */?> <div class="post article feature">
在显示之前检查属性值
例如,使用来自Web API的JSON。
$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();
显示
<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>
使用已转义的信息
$escapedText = 'Bangers & Mash'; GlazeServe::attribute('alt', $escapedText, Glaze\TYPE_PREGLAZED); /* Displays: */?> alt="Bangers & Mash"