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

BbCode View

Daniel Hood

Well-known member
#1
How do I include a view in my bbcode call so that the templates get preloaded and everything?

Currently in my model for getting posts I have
$bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base'));
But quotes for example aren't rendering. I'm under the impressions it'll be something like

$bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base', array('view' => new XenForo_ViewPublic_Post_Quote)));
But that doesn't work and I can't tell how far off I am.
 

Chris D

XenForo developer
Staff member
#2
You shouldn't really be calling the BB Code parser in a model.

I presume you've got a controller that is ultimately calling something like:

PHP:
    public function actionTest()
    {
        $postModel = XenForo_Model::create('XenForo_Model_Posts');

        $viewParams = array(
            'posts' => $postModel->getPosts()
        );

        return $this->responseView('YourAddOn_ViewPublic_Posts', 'your_template', $viewParams);
    }
"YourAddOn_ViewPublic_Posts" is a view class that can usually either exist or not. Most of the time we don't need to do anything in the view. But if you're doing something with BB Code then doing it in a view class is the most appropriate.

So you now need:

PHP:
<?php

class YourAddOn_ViewPublic_Posts extends XenForo_ViewPublic_Base
{
    public function renderHtml()
    {
        $bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));

        XenForo_ViewPublic_Helper_Message::bbCodeWrapMessages($this->_params['posts'], $bbCodeParser);
    }
}
Does that make sense?

The parsed BB code is then available in your template using:

Code:
<xen:foreach loop="$posts" value="$post">
    {xen:raw $post.messageHtml}
</xen:foreach>