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

Parse BBcode in Model instead of View?

Discussion in 'XenForo Development Discussions' started by Jaxel, Dec 8, 2014.

  1. Jaxel

    Jaxel Well-Known Member

    I know I can parse bbcode in a text field in the view public using this:
    Code:
    $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
    $field['html'] = new XenForo_BbCode_TextWrapper($field['text'], $bbCodeParser);
    Is it possible to parse the bbcode for a string in the model instead of the view?
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    Here may be working :)
    PHP:
    $request = new Zend_Controller_Request_Http();
    $response = new Zend_Controller_Response_Http();

    $dependencies = new XenForo_Dependencies_Public();

    $viewRenderer = new XenForo_ViewRenderer_Abstract($dependencies$response$request);
    $view = new XenForo_View($viewRendererresponse);

    $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $view)));
    $field['html'] = new XenForo_BbCode_TextWrapper($field['text'], $bbCodeParser);

     
  3. Jaxel

    Jaxel Well-Known Member

    Seems like a lot of "new" in there. I wonder how that affects performance.
     
  4. Xon

    Xon Well-Known Member

    You only need to pass in a View, when dealing with bbcode which uses templates. You can also reuse the View object and formatter object between XenForo_BbCode_Parser instances.

    This is done a bunch of time in the XenForo codebase.

    Code:
    public function sendNotificationToWatchUsersOnReply(array $reply, array $thread = null, array $noAlerts = array())
    {
    ...
                    if (!isset($reply['messageText']) && $parseBbCode)
                    {
                        $bbCodeParserText = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Text'));
                        $reply['messageText'] = new XenForo_BbCode_TextWrapper($reply['message'], $bbCodeParserText);
    
                        $bbCodeParserHtml = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('HtmlEmail'));
                        $reply['messageHtml'] = new XenForo_BbCode_TextWrapper($reply['message'], $bbCodeParserHtml);
                    }
    ...
    }
    
     
    Last edited: Dec 10, 2014
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    Yep! Maybe it not good but if you simply to render so using post #3 :)
     
  6. Jaxel

    Jaxel Well-Known Member

    Some bbcodes only parse if they have a $view. So when you parse the bbcode in a model, it doesnt parse specific codes.
     
  7. cclaerhout

    cclaerhout Well-Known Member

    Parse the Bb Codes in the view, that will be the cleanest and safest solution. There are some workarounds, but I don't see why you would have to use it if you code something yourself and not try to extend a XenForo feature that parse the Bb Codes without the view (as Xon said, it happens sometimes).

    If you do need a workaround, play with the controller using the controller pre view listener, generate the view (before XenForo generates it just after ; which means this task would be done twice instead of once)
    PHP:
    protected static $_view;

    public static function 
    controllerPreView(XenForo_FrontController $fc,
       
    XenForo_ControllerResponse_Abstract &$controllerResponse,
       
    XenForo_ViewRenderer_Abstract &$viewRenderer,
       array &
    $containerParams
    )
    {
       if(!
    $viewRenderer instanceof XenForo_ViewRenderer_Json)
       {
         
    $response $fc->getResponse();
         
    self::$_view = new XenForo_ViewPublic_Base($viewRenderer$response$containerParams);
       }
    }
    Once you get the view, just save it somewhere. You can't set a session variable [XenForo_Application::getSession()->set('myVariable', $data);] ; this pre view listener occurs too late ; it can only be done during the controller pre dispatch. But you should be able to set this variable using a XenForo application [XenForo_Application::set('myVariable', $data);]. Again, this workaround should be avoided.
     
    Insy and Nobita.Kun like this.

Share This Page