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

Discussion in 'XenForo Development Discussions' started by account8226, Dec 16, 2012.

  1. account8226

    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.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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);
    }
     
    Lil.Tee and (deleted member) like this.
  3. account8226

    account8226 Guest

    I am doing this in my Public controller, and I am sending the $message param to my template.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    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;
        }
    }
     
    Lil.Tee and (deleted member) like this.
  5. account8226

    account8226 Guest

    Thanks dude, I made my ViewPublic class, and how do I call it in my public controller, I don't really get the goal of a viewpublic class.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    Lil.Tee and (deleted member) like this.
  7. account8226

    account8226 Guest

    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
    );
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Is $messages defined in your ViewPublic class at all?

    What does the code for your ViewClass look like?
     
    Elly likes this.
  9. account8226

    account8226 Guest

    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) !
     
  10. account8226

    account8226 Guest

    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.
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  12. account8226

    account8226 Guest

    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] !
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    Hmm

    And what output do you get? Anything?
     
  14. account8226

    account8226 Guest

    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.
     
  15. Chris D

    Chris D XenForo Developer Staff Member

    Just remembered...

    To stop the characters being escaped, you need to call your parameter like this:

    {xen:raw $message.content}
     
    Romchik® and (deleted member) like this.
  16. account8226

    account8226 Guest

    Amazing! It's great, working perfectly :) Thanks again and again for your help Chris! (y)(y)(y)
     
    Chris D likes this.

Share This Page