Zend_Markup se distribuye actualmente con un renderer, el
renderer HTML.
Al añadir sus propios markups, puede añadir su propia funcionalidad a los renderers de
Zend_Markup. Con la estructura de markup, puede añadir
prácticamente cualquier funcionalidad que desee. Desde markups simples, hasta
estructuras de markup complicadas. Un ejemplo simple para un markup 'foo':
// Creates instance of Zend_Markup_Renderer_Html,
// with Zend_Markup_Parser_BbCode as its parser
$bbcode = Zend_Markup::factory('Bbcode');
// this will create a simple 'foo' markup
// The first parameter defines the markup's name.
// The second parameter takes an integer that defines the markups type.
// The third parameter is an array that defines other things about a
// markup, like the markup's group, and (in this case) a start and end markup.
$bbcode->addMarkup(
'foo',
Zend_Markup_Renderer_RendererAbstract::TYPE_REPLACE,
array(
'start' => '-bar-',
'end' => '-baz-',
'group' => 'inline'
)
);
// now, this will output: 'my -bar-markup-baz-'
echo $bbcode->render('my [foo]markup[/foo]');
Tenga en cuenta que crear sus propios markups solo tiene sentido cuando su parser también lo admite con una estructura de markup. Actualmente, solo BBCode admite esto.
Algunos renderers (como el renderer HTML) también tienen soporte para un parámetro 'markup'. Este reemplaza los parámetros 'start' y 'end', y renderiza los markups incluyendo algunos atributos por defecto y el markup de cierre.
Al añadir un markup callback, puede hacer mucho más que un simple reemplazo de los markups. Por ejemplo, puede cambiar el contenido, usar los parámetros para influir en la salida, etc.
Un callback es una clase que implementa la interfaz
Zend_Markup_Renderer_TokenInterface. Un ejemplo de una
clase callback:
class My_Markup_Renderer_Html_Upper
implements Zend_Markup_Renderer_TokenConverterInterface
{
public function convert(Zend_Markup_Token $token, $text)
{
return '!up!' . strtoupper($text) . '!up!';
}
}
Ahora puede añadir el markup 'upper', con una instancia de la clase
My_Markup_Renderer_Html_Upper como callback. Un ejemplo
simple:
// Creates instance of Zend_Markup_Renderer_Html,
// with Zend_Markup_Parser_BbCode as its parser
$bbcode = Zend_Markup::factory('Bbcode');
// this will create a simple 'foo' markup
// The first parameter defines the markup's name.
// The second parameter takes an integer that defines the markups type.
// The third parameter is an array that defines other things about a
// markup, like the markup's group, and (in this case) a start and end markup.
$bbcode->addMarkup(
'upper',
Zend_Markup_Renderer_RendererAbstract::TYPE_CALLBACK,
array(
'callback' => new My_Markup_Renderer_Html_Upper(),
'group' => 'inline'
)
);
// now, this will output: 'my !up!MARKUP!up!'
echo $bbcode->render('my [upper]markup[/upper]');
Tabla 46.1. Lista de markups
| Entrada de ejemplo (bbcode) | Salida de ejemplo |
|---|---|
| [b]foo[/b] | <strong>foo</strong> |
| [i]foo[/i] | <em>foo</em> |
| [cite]foo[/cite] | <cite>foo</cite> |
| [del]foo[/del] | <del>foo</del> |
| [ins]foo[/ins] | <ins>foo</ins> |
| [sup]foo[/sup] | <sup>foo</sup> |
| [sub]foo[/sub] | <sub>foo</sub> |
| [span]foo[/span] | <span>foo</span> |
| [acronym title="PHP Hypertext Preprocessor]PHP[/acronym] | <acronym title="PHP Hypertext Preprocessor">PHP</acronym> |
| [url=http://framework.zend.com/]Zend Framework[/url] | <a href="http://framework.zend.com/">Zend Framework</a> |
| [h1]foobar[/h1] | <h1>foobar</h1> |
| [img]http://framework.zend.com/images/logo.gif[/img] | <img src="http://framework.zend.com/images/logo.gif" /> |