1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Render BBCode in Notices

Discussion in 'XenForo Development Discussions' started by Robust, Jan 9, 2016.

  1. Robust

    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.
     
  2. Robust

    Robust Well-Known Member

    What I'm doing:
    [​IMG]

    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:
    [​IMG]
    (note "p" is dumped at the top, too, removed when I remove the parser lines)
     
  3. Robust

    Robust Well-Known Member

    I got the parser working by using {xen:raw $param} instead. It's still dumping 'p' though, for some reason.
     
  4. Xon

    Xon Well-Known Member

    @Robust instead of 'new XenForo_BbCode_Parser' you should use:
    Code:
    $parser = XenForo_BbCode_Parser::create($formatter);
    
     
  5. Robust

    Robust Well-Known Member

    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.
     
  6. katsulynx

    katsulynx Well-Known Member

    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.
     
    Xon likes this.
  7. Robust

    Robust Well-Known Member

    Yup, that's what I'm doing
     
  8. cclaerhout

    cclaerhout Well-Known Member

    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.
     
    Robust likes this.
  9. Robust

    Robust Well-Known Member

    Thanks! :)

    Just took a look. I guess the view is required to not mess up. I'll implement that and let you know.
     
    cclaerhout likes this.
  10. Robust

    Robust Well-Known Member

    @cclaerhout I still see 'p' dumped to the top of the page, however it does work.
     
  11. Robust

    Robust Well-Known Member

    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);
            }
        }
     
  12. cclaerhout

    cclaerhout Well-Known Member

    Sorry no time to check your code, but be sure to take a look at this helper; it's working with the Github file I gave you above.
     
  13. wang

    wang Well-Known Member

    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.
     
  14. cclaerhout

    cclaerhout Well-Known Member

    Using the Bb Codes parser after a content has been parsed in hml requires to use workarounds (see my previous message).
     
  15. Robust

    Robust Well-Known Member

    Still doesn't seem to work. I did the pre-view and the helpers.
     
  16. Robust

    Robust Well-Known Member

    Definitely isn't working, triple checked it all and made sure to use the helpers, too.
     
  17. cclaerhout

    cclaerhout Well-Known Member

    If you use my addon, can you reproduce the problem?
     

Share This Page