Skip to content

Instantly share code, notes, and snippets.

@jbowens
Created January 7, 2014 21:47
Show Gist options
  • Save jbowens/8307499 to your computer and use it in GitHub Desktop.
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.
<?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