Need help creating custom CodeDefinition
Hi,
i want to creade BBCode which looks like this:
[insertimg imgid=22 style=left border=1 alt="alttext"]
Point is, instead of image url, i need image id so I can buildimage name (finally, I should read image info from database) and provide proper HTML markup to show that image.
I made more or less everything that I needed except, i cannot use this BBCode without closing tag [/insertimg]. i want to get rid of that closing tag. I read all I could and searched for answer but could not get it.
Basicaly, now I have to put tag in this form:
[insertimg imgid=22 style=left border=1 alt="alttext"][/insertimg]
but i want it to be simpler, this form:
[insertimg imgid=22 style=left border=1 alt="alttext"]
Here is my code:
class bbcInsertImg extends \JBBCode\CodeDefinition {
public function __construct() {
$this->parseContent = false;
$this->useOption = true;
$this->setTagName('insertimg');
$this->nestLimit = -1;
}
public function asHtml(\JBBCode\ElementNode $pElement) {
$m_attributes = $pElement->getAttribute();
if (isset ($m_attributes['imgid'])) {
$img_url = $m_attributes['imgid'] . '.jpg';
} else {
$img_url = '';
}
$bodyHtml = '<img src="' . $img_url . '"';
$bodyHtml .= $this->AttribToHTML($pElement, 'style');
$bodyHtml .= $this->AttribToHTML($pElement, 'border');
$bodyHtml .= $this->AttribToHTML($pElement, 'alt');
$bodyHtml .= '>';
return $bodyHtml;
}
private function AttribToHTML($pElement, $pAttrib, $pDefaultValue = '') {
$m_attributes = $pElement->getAttribute();
if (isset ($m_attributes[$pAttrib])) {
$m_result = ' ' . $pAttrib . '="' . $m_attributes[$pAttrib] . '"';
} else {
if (strlen ($pDefaultValue) > 0) {
$m_result = ' ' . $pAttrib . '="' . $pDefaultValue . '"';
} else {
$m_result = '';
}
}
return $m_result;
}
}
I am still struggling with this issue. Can anybody give me some hint?
@pedjas As far as I know, jBBCode does not have support for unary tags yet. @jbowens Probably this one should be labeled as #bug, shouldn't it?
Until support for unary tags is added, why not use the positive qualities of the closed-tag format to your benefit?
[includeimg=22 style=left border=1]This is my alt text![/includeimg]