XF 2.0 BBCode to HTML in PHP

Jean-Baptiste

Well-known member
Hello,

I would like in my PHP code to render the template by parsing every BBcode to it's equivalent in HTML.

How can I do that ?

Best regards :)
 

Mike

XenForo developer
Staff member
In a template, you just use the bb_code function. This is how it's done for posts:
Code:
{{ bb_code($post.message, 'post', $post) }}
 

Jeremy P

Well-known member
PHP:
$formatter = \XF::app()->stringFormatter();
$html = $formatter->replaceSmiliesHtml(':)');
 
PHP:
$formatter = \XF::app()->stringFormatter();
$html = $formatter->replaceSmiliesHtml(':)');
Hi Jeremy :)

I guess I do not know this job :)

$reply->getParams('thread')['posts']; Get All Thread Post..

But Clear text. Not HTML..

How to get HTML Post List??

Please help me :D 3 I could not make the day, so I could not get answers.
 
\XF\Mvc\Reply\AbstractReply $reply

\XF::dump($reply);

Code:
"message" => """

lorem ipsum dolor sid [URL='http://localhost']amed [/URL] lorem ipsum dolor sid amed.\n
@Jeremy P
 

Jeremy P

Well-known member
Oh, my answer was only about rendering smilies. For converting a given post from BBCode to HTML:
PHP:
$html = \XF::app()->bbCode()->render(
    $post->message,
    'html',
    'post',
    $post
);
 
Oh, my answer was only about rendering smilies. For converting a given post from BBCode to HTML:
PHP:
$html = \XF::app()->bbCode()->render(
    $post->message,
    'html',
    'post',
    $post
);
Can use I it here?

function public threadControllerPostDispatch(\XF\Mvc\Dispatcher $dispatcher, &$content, \XF\Mvc\Reply\AbstractReply $reply, \XF\Mvc\Renderer\AbstractRenderer $renderer, \XF\Http\Response $response)
 
Hi Jeremy again..

i using :

using $reply->getParams('thread')['posts'];


But BBCode not rendering..

I have to do this after the BBcode Renderer is done.
 
Last edited:

Jeremy P

Well-known member
What are you trying to accomplish? The template/macro that renders posts is expecting the post to be in BBCode markup during evaluation. You can't convert it or make HTML changes during the controller life-cycle because that runs prior to the template evaluation.
 
What are you trying to accomplish? The template/macro that renders posts is expecting the post to be in BBCode markup during evaluation. You can't convert it or make HTML changes during the controller life-cycle because that runs prior to the template evaluation.
How can I run it afterwards?
I want to process the rendered reply.
 

Jeremy P

Well-known member
Write a custom callback method or template function that does whatever you need, and wrap the bb_code() call in the post macros template with it.
 

Jeremy P

Well-known member
Add a templater_setup event listener with a callback method:

PHP:
public static function templaterSetup(
    \XF\Container $container,
    \XF\Template\Templater &$templater
) {
    $templater->addFunction('foo_bar', function (
        \XF\Template\Templater
        $templater,
        &$escape,
        $html
    ) {
        $escape = false;

        // do whatever with $html

        return $html;
    });
}
Then you can wrap any bb_code() function calls in templates to make the HTML changes you need:
HTML:
{{ foo_bar(bb_code($post.message, 'post', $post)) }}
 

_PkZ_

Active member
Then you can wrap any bb_code() function calls in templates to make the HTML changes you need:
Is it possible to add a function that are called without the need to add it to the template?
I am trying alter the $post just before it is rendered for the display.
 

Jeremy P

Well-known member
If you're trying to alter the message in HTML form, no. The template expects to receive it in BBCode form.
 
Top