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

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

A

account8226

Guest
#1
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.
 

Chris D

XenForo developer
Staff member
#2
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);
}
 
A

account8226

Guest
#3
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.
 

Chris D

XenForo developer
Staff member
#4
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;
	}
}
 

Chris D

XenForo developer
Staff member
#6
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.
 
A

account8226

Guest
#7
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
);
 
A

account8226

Guest
#9
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) !
 
A

account8226

Guest
#10
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.
 

Chris D

XenForo developer
Staff member
#11
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.
 
A

account8226

Guest
#12
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] !
 
A

account8226

Guest
#14
For example, with the content :

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

297e2644a76ab38c18ed86e0ef1971d5.png

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.