Render BBCode in Notices

Robust

Well-known member
How do you go about rendering BBCode in notices? They can be global, so there's not exactly a view for it that you can render in. If I add the parser into the place where I hook into notices_prepare I just get 'p' dumped to the top of the page.
 
What I'm doing:
50855e204d.png


Code:
            $formatter = XenForo_BbCode_Formatter_Base::create('Base');
            $parser = new XenForo_BbCode_Parser($formatter);

            foreach($tasks as $task)
            {
                $tasks[$task['task_id']]['title'] = $parser->render($task['title']);
            }

            $template->setParam('tasks', $tasks);
            $template->setParam('completed', $completed);

Output:
d6a00de3bb.png

(note "p" is dumped at the top, too, removed when I remove the parser lines)
 
I got the parser working by using {xen:raw $param} instead. It's still dumping 'p' though, for some reason.
 
@Robust instead of 'new XenForo_BbCode_Parser' you should use:
Code:
$parser = XenForo_BbCode_Parser::create($formatter);
 
@Robust instead of 'new XenForo_BbCode_Parser' you should use:
Code:
$parser = XenForo_BbCode_Parser::create($formatter);
Oh yeah, I actually changed that already - I think I did "new" out of habit or something. It didn't make much of a difference.
 
Spontaneously looking at it makes me think of an error in the template. You should be using {xen:raw $yourcontentvariable} there to have it fully rendered.
 
  • Like
Reactions: Xon
How do you go about rendering BBCode in notices? They can be global, so there's not exactly a view for it that you can render in. If I add the parser into the place where I hook into notices_prepare I just get 'p' dumped to the top of the page.
There's no clean and easy way to do this, you need to get the view before calling the "notices_prepare" hook. This addon does this. Look at this file on Github.
 
Relevant code:

Code:
    public static function add_notice(array &$noticeList, array &$noticeTokens, XenForo_Template_Abstract $template, array $containerData)
    {
        $visitor = XenForo_Visitor::getInstance();
        $options = XenForo_Application::getOptions();
        $bbCodeMode = false;

        /* @var $progressModel Apantic_UserProfileProgress_Model_UserProfileProgress */
        $progressModel = XenForo_Model::create('Apantic_UserProfileProgress_Model_UserProfileProgress');

        $tasks = $progressModel->getTasks(array('active' => 1));

        if($options->auppEnableViewHook)
        {
            $bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base', array(
                'view' => self::$_view
            )));

            $bbCodeMode = true;
        }

        if(!empty($visitor['user_id'])
            && $visitor['aupp_percent_completed'] < 100
            /* && $visitor->hasPermission('', '')*/
        )
        {

            $completed = $progressModel->getTasksCompletedByUser($visitor);
            if(empty($tasks))
            {
                return;
            }

            if($bbCodeMode)
            {
                foreach($tasks as $task)
                {
                    $tasks[$task['task_id']]['title'] = $bbCodeParser->render($task['title']);
                }
            }

            $template->setParam('tasks', $tasks);
            $template->setParam('completed', $completed);

            $noticeList['auppPendingTasks'] = array(
                'title' => new XenForo_Phrase('aupp_notice_title'),
                'message' => $template->create('aupp_notice_tasks', $template->getParams()),
                'wrap' => true,
                'dismissible' => false,
                'delay_duration' => '',
                'display_duration' => '',
                'auto_dismiss' => false,
                'display_image' => '',
                'display_style' => 'primary',
                'css_class' => '',
                'visibility' => '',
                'notice_type' => ($options->auppNoticeLocation == 'floating' ? 'floating' : 'block')
            );

            $tokens = array(
                '{progress}' => $visitor['aupp_percent_completed'],
            );

            $noticeTokens = array_merge($noticeTokens, $tokens);
        }
    }

Code:
    public static function controllerPreView(XenForo_FrontController $fc,
                                             XenForo_ControllerResponse_Abstract &$controllerResponse,
                                             XenForo_ViewRenderer_Abstract &$viewRenderer,
                                             array &$containerParams
    )
    {
        if(!($viewRenderer instanceof XenForo_ViewRenderer_Json)
            && XenForo_Application::get('options')->get('auppEnableViewHook')
        )
        {
            $response = $fc->getResponse();
            self::$_view = new XenForo_ViewPublic_Base($viewRenderer, $response, $containerParams);
        }
    }
 
I do not see anything in the code that could cause that p to appear. Can you please post the contents of the template that you are using?

Another thing to check for, would be white spaces in the php files.
 
Back
Top Bottom