III. Using NBBC
[ Previous: B. How a Typical NBBC Program Works | Next: D. Adding Your Own Smileys ]
C. Adding Your Own Tags
Let's say you want to add a BBCode tag that doesn't exist, say, how about, a [mono]
for displaying things as monospace text? Let's look at how you'd go about adding a
simple additional tag.
To add a tag, which NBBC calls a "rule," since it's really a rule for describing how
a given chunk of input is processed, you simply call the AddRule
method of the $bbcode object and pass to it an array describing how the new
rule will convert its input to HTML. Here's an example for our [mono] tag:
'',
'simple_end' => '',
'class' => 'inline',
'allow_in' => Array('listitem', 'block', 'columns', 'inline', 'link'),
);
$bbcode->AddRule('mono', $new_rule);
...
$input = "This text is [mono]monospaced![/mono]";
$output = $bbcode->Parse($input);
print $output;
?>
This text is monospaced!
Each new rule is described by an array of parameters. There are a large number of
different parameters you can use, but for now, let's look at just the four parameters
shown in this example. (You can learn about the rest of the parameters in the appendix
on BBCode rule parameters.)
The four parameters
given here are:
- simple_start: This describes some HTML to be added in place of the
starting [mono] tag. In this case, we're going to replace it with
the <tt> HTML element.
- simple_end: This describes some HTML to be added in place of the
ending [/mono] tag. In this case, we're going to replace it with
the </tt> HTML element terminator.
- class: This assigns this new rule and its contents to be of a certain
"class" of data within the document. Classes control which tags are allowed to
go inside which other tags, and ensure that the resulting HTML is legal and
valid. The standard classes are: block, inline, list,
listitem, link, columns, nextcol, code,
and image. (See the appendix on content
classes for more details.) In this case, our new tag will be of class
inline; inline is used to describe a chunk of text in a
paragraph. Most tags that change the appearance of text, like [b]
and [i] and [font], are of class inline also.
- allow_in: This is a list of which tag classes this new tag may
be used inside. It can be safely used inside list items, like [*],
inside block items like [center], and inside other inline items,
like [b]. For a complete list of the tag classes and how they relate
to each other, see the appendix on content classes.
Adding the new rule is very straightforward once the rule is defined; you just call
BBCode::AddRule and pass it the name of the new rule and the array containing
its parameters. Advanced users may prefer to write it like this instead, though, because
it's actually a little faster and doesn't pollute the namespace with an unnecessary
$new_rule variable that you'll never use again:
$bbcode->AddRule('mono', Array(
'simple_start' => '',
'simple_end' => '',
'class' => 'inline',
'allow_in' => Array('listitem', 'block', 'columns', 'inline', 'link'),
));
[ Previous: B. How a Typical NBBC Program Works | Next: D. Adding Your Own Smileys ]
Copyright © 2010, the Phantom Inker. All rights reserved.