Created
January 7, 2014 21:47
-
-
Save jbowens/8307499 to your computer and use it in GitHub Desktop.
A jBBCode Code Definition for an image bbcode that supports an option class name argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "jBBCode" . DIRECTORY_SEPARATOR . "Parser.php"; | |
/** | |
* Implements an [img=alt] tag that supports an optional class argument. | |
* | |
*/ | |
class ImageWithClass extends \JBBCode\CodeDefinition | |
{ | |
public function __construct() | |
{ | |
$this->parseContent = false; | |
$this->useOption = true; | |
$this->setTagName('img'); | |
$this->nestLimit = -1; | |
} | |
public function asHtml(\JBBCode\ElementNode $el) | |
{ | |
$url = ''; | |
foreach ($el->getChildren() as $child) { | |
$url .= $child->getAsText(); | |
} | |
// Split the argument on the pipe character | |
$argPieces = explode('|', $el->getAttribute()); | |
$altText = $argPieces[0]; | |
$class = 'default-class'; | |
if (count($argPieces) > 1) { | |
$class = $argPieces[1]; | |
} | |
return '<img src="' + $url + '" alt="' + $altText + '" class="' + $class + '" />'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment