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

Trim and BBCode

Discussion in 'XenForo Development Discussions' started by Mythotical, Apr 29, 2011.

  1. Mythotical

    Mythotical Well-Known Member


    In my portal system I need to trim latest news article to a certain amount of characters before the read more link appears, but I can't figure that out as well as I need to convert bbcode for displaying on my portal.

    Here is my latest news model:
    PHP:
    class xPortal_Model_News extends XenForo_Model
    {
        public function 
    getNews()
        {
            
    $nOption XenForo_Application::get('options')->xportal_news;

            
    $nThread $this->limitQueryResults('
                SELECT
                    thread.title, thread.first_post_id, thread.username, post.*, user.*
                FROM xf_thread AS thread
                LEFT JOIN xf_post AS post ON (post.post_id = thread.first_post_id)
                LEFT JOIN xf_user AS user ON (user.username = thread.username)
                WHERE thread.node_id = ?
            '
    $nOption);

            return 
    $this->_getDb()->fetchRow($nThread$nOption);
        }
    }
    Same thing for my latest posts. Here is my latest posts model:
    PHP:
    class xPortal_Model_LatestPosts extends XenForo_Model
    {
        public function 
    getLatestPosts()
        {
            
    $lPosts $this->limitQueryResults('
                SELECT post.*, thread.*
                FROM xf_post AS post
                LEFT JOIN xf_thread AS thread ON (thread.thread_id = post.thread_id)
                WHERE message_state = "visible"
                ORDER BY post.post_date
            '
    5);

    return 
    $this->_getDb()->fetchAll($lPosts);
        }
    }
    Thanks in advance
     
  2. James

    James Well-Known Member

    IIRC, isn't there a {xen:snip} you can use in templates to snip the code?
     
  3. Bob

    Bob Well-Known Member

    this one?

    {xen:helper snippet, $var.field, 25} where 25 is the max number of characters to display
     
  4. James

    James Well-Known Member

    I'm not sure but that seems accurate. If you search for 'snippet' in your templates it'll probably return a result.

    I was thinking instead of trimming in your code, you could just trim in your template using native XenForo syntax.
     
  5. ragtek

    ragtek Guest

    That's what i'm using in my global feed add-on:

    PHP:
    $message XenForo_Helper_String::wholeWordTrim($thread['message'], $descriptionLenght);
    $message XenForo_Helper_String::bbCodeStrip($message);
     
  6. Mythotical

    Mythotical Well-Known Member

    Thanks James and bobster. Thanks Ragtek for that code snippet.
     
  7. Mythotical

    Mythotical Well-Known Member

    I used the following:

    HTML:
    {xen:helper snippet, $post.message, 30}
    So snippet exists. Thanks for pointing that out.
     
  8. Mythotical

    Mythotical Well-Known Member

    What about converting BBCode?
     
  9. ragtek

    ragtek Guest

    Start reading the code:p

    helperSnippet (that's what's called when you use the 'snippet' helper) has an options parameter as 3rd parameter which allows you to strip the bbcode & do other cool stuff...!

    PHP:
    public static function helperSnippet($string$maxLength 0, array $options = array())
        {
            
    $options array_merge(array(
                
    'term' => '',
                
    'emClass' => '',
                
    'stripQuote' => false
            
    ), $options);

            
    $string XenForo_Helper_String::bbCodeStrip($string$options['stripQuote']);

            if (
    $maxLength)
            {
                
    $string XenForo_Helper_String::wholeWordTrimAroundSearchTerm($string$maxLength$options['term']);
            }

            
    $string trim($string);
            
    $string XenForo_Helper_String::censorString($string);

            if (
    $options['term'] && $options['emClass'])
            {
                return 
    XenForo_Helper_String::highlightSearchTerm($string$options['term'], $options['emClass']);
            }
            else
            {
                return 
    htmlspecialchars($string);
            }
        }
     
    Kier likes this.
  10. Mythotical

    Mythotical Well-Known Member

    When it comes to the helper stuff I never figured they would have extra options. Thanks for pointing it out.

    I'm playing with the new cool toy you just showed me. LOL
     
  11. Mythotical

    Mythotical Well-Known Member

    Ok I read the code, played with it, but I can't see anyway to use snippet to convert bbcode, trim works fine though.
     
  12. Mythotical

    Mythotical Well-Known Member

    Anyone able to give help with the BBCode portion of helperSnippet?
     
  13. ragtek

    ragtek Guest

    {xen:helper snippet, $post.message, 100, {xen:array 'stripQuote=1'}}
     
  14. Mythotical

    Mythotical Well-Known Member

    I used that but it only displays the beginning IMG tag, is that how it should work?
     
  15. ragtek

    ragtek Guest

    lol
    ok i think i've misunderstood you.

    YOu want to PARSE it and not to remove it, right?:D

    then you'll need the helperBbCode helper method
     
  16. Mythotical

    Mythotical Well-Known Member

    Correct. LOL

    I tried using the helperBbCode within helperSnippet but got nothing so I'm not even sure where I"m messing up at.
     
  17. ragtek

    ragtek Guest

    O, cmon!
    Start reading the code:p
     
  18. Mythotical

    Mythotical Well-Known Member

    I have read the code and re-read it. Let me get some more sleep and I'll attempt again and provide the code I'm using here.
     
  19. Mythotical

    Mythotical Well-Known Member

    Ok Ragtek, I threw it together again, here is the error I get:
    This is what I have in the template:
    HTML:
    {xen:helper BbCode, $parser, $lPosts}

    I have also did this:
    HTML:
    {xen:helper snippet, {xen:helper BbCode, $parser, $lPosts}, $trim}

    Still get the same error above. I read the code but I assume $parser is not correct and I'm at a loss of what the correct variable would be.
     
  20. James

    James Well-Known Member

    Did you try using the $post.message? I noticed it wasn't in your last conditional.
     

Share This Page