Parse BB Code (or html) in a <xen:foreach> loop

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
Hello,

I am trying to parse the result of a text storred in my database, I tried to stored the text as BB Code and then HTML.

I would like to parse the content stored in my database.

I tried with the following code :
PHP:
foreach ($lastMessages AS $key => $mess)
{
    $formatter = XenForo_BbCode_Formatter_Base::create();
    $parser = new XenForo_BbCode_Parser($formatter);
    $message[$key]['content'] = $parser->render("[b]This is a just a <i>test</i>[/b]");
}

Then I send my object within the $viewParams parameters.

But it looks like in my template, xen:foreach is escaping my chars (with htmlspecialchars).

How could I parse my text to my template ?

Best regards.
 
What are you sending back in your $viewParams paramter?

$message or $lastMessages?

Also, are you doing this in your controller, or the view?

Finally, I tend to process BB Code more like the following. This is a recent example from something I'm working on:
PHP:
$bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));

foreach ($comments AS $key => &$comment)
{
	$comments[$key]['message'] = new XenForo_BbCode_TextWrapper($comments[$key]['message'], $bbCodeParser);
}
 
What are you sending back in your $viewParams paramter?

$message or $lastMessages?

Also, are you doing this in your controller, or the view?

Finally, I tend to process BB Code more like the following. This is a recent example from something I'm working on:
PHP:
$bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
 
foreach ($comments AS $key => &$comment)
{
$comments[$key]['message'] = new XenForo_BbCode_TextWrapper($comments[$key]['message'], $bbCodeParser);
}

I am doing this in my Public controller, and I am sending the $message param to my template.
 
Ok. So you know how your responseView looks something like this?:

PHP:
return $this->responseView('YourAddOn_ViewPublic_AddOn', 'youraddon_template', $viewParams);

Well, usually the first parameter of responseView (YourAddOn_ViewPublic_AddOn) is a dummy class that doesn't need any PHP writing for it. But this can be used to define your View class. And it's usually your view class where you alter the output of your template.

So you will need an additional PHP file, that looks like this:

PHP:
<?php

class YourAddOn_ViewPublic_AddOn extends XenForo_ViewPublic_Base
{
	public function renderHtml()
	{
		$bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
		
		$message = $this->_params['message']; // This pulls the $message parameter from the viewParams you pass from the controller.
		
		foreach ($message AS $key => &$mess) // Just me being pedantic, but consider using the variable name "$messages". It doesn't make any difference.
		{
			$message[$key]['content'] = new XenForo_BbCode_TextWrapper($message[$key]['content'], $bbCodeParser);
		}
		
		$this->_params['message'] = $message;
	}
}
 
The View class enables you to modify the output of the view.

Your controller simply calls the view (i.e. the template and its parameters), but it's the view class that actually makes it display and render. Usually, we do not need to specify an actual ViewPublic class.

Is it the same Controller you needed help with yesterday?

If so, look at this:
PHP:
return $this->responseView('Leaderboars_ViewPublic_Home', 'leaderboards', $viewParams);

"Leaderboars_ViewPublic_Home" is your view class.
"leaderboards" is your template.
$viewParams is your parameters to be passed to the template.

Obviously Leaderboars_ViewPublic_Home doesn't actually exist. It's a dummy class. Using a dummy class means there's not going to be any further modifications to the view and things just render in the default XenForo view. This is usually sufficient.

However, if you define there an actual class that exists then that will extend the default class.

So, with that in mind, your controller should return something like this:

PHP:
return $this->responseView('XenModeration_ViewPublic_Index', 'leaderboards', $viewParams);

Where "XenModeration_ViewPublic_Index" is the real class name of your ViewPublic PHP file.
 
Yes, I think I understood that, but I have the following error :
Code:
An exception occurred: Undefined index: messages

Here is my viewParams var :
PHP:
$messages = $lastMessages; /* where $lastMessages is an object, $lastMessages['creator'] got the creator UID, and $lastMessages['content'] got the message I would like to parse */
$viewParams = array(
'lastmessages' => $messages,
'staffmembers' => $staff
);
 
Is $messages defined in your ViewPublic class at all?

What does the code for your ViewClass look like?
 
It's ok I am stupid ;)

PHP:
$messages = $lastMessages; /* where $lastMessages is an object, $lastMessages['creator'] got the creator UID, and $lastMessages['content'] got the message I would like to parse */
$viewParams = array(
'messages' => $messages,
'staffmembers' => $staff
);

Here is the correct code :)

My ViewPublic class need 'messages' and not $messages ^^

Thanks again for your help Chris :) (y) !
 
Is $messages defined in your ViewPublic class at all?

What does the code for your ViewClass look like?

It's still not working (working, but not parsing in my template) :cry: .

Here is my ViewPublic :
PHP:
class XenModeration_ViewPublic_Index extends XenForo_ViewPublic_Base
{
    public function renderHtml()
    {
        $bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
     
        $message = $this->_params['messages']; // This pulls the $message parameter from the viewParams you pass from the controller.
     
        foreach ($message AS $key => &$mess) // Just me being pedantic, but consider using the variable name "$messages". It doesn't make any difference.
        {
            $message[$key]['content'] = new XenForo_BbCode_TextWrapper($message[$key]['content'], $bbCodeParser);
        }
     
        $this->_params['messages'] = $message;
    }
}

If I add an "echo" in the foreach loop, I can see at the top of my forum that it's correctly parsing.
 
Ok.
View looks good. Controller looks good.

Can I see your template code?

This should all be working so I can only assume there is something wrong there.
 
Here it is :
Code:
<div class="messagesList">
    <xen:foreach loop="$messages" value="$message">
        {$message.creator}
        {$message.content}
    </xen:foreach>
</div>

And here one of the content raw :
Code:
Hi [b]guys[/b] !
 
For example, with the content :

Code:
Hi [b]guys[/b] !

I have :

297e2644a76ab38c18ed86e0ef1971d5.webp

Don't you think it is like rich username in the user params ? For example {$user.username} in message_user_info won't be colored.
 
Back
Top Bottom