Trim and BBCode

Cupara

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
 
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.
 
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);
 
I used the following:

HTML:
{xen:helper snippet, $post.message, 30}

So snippet exists. Thanks for pointing that out.
 
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);
        }
    }
 
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
 
Ok I read the code, played with it, but I can't see anyway to use snippet to convert bbcode, trim works fine though.
 
{xen:helper snippet, $post.message, 100, {xen:array 'stripQuote=1'}}
 
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
 
Correct. LOL

I tried using the helperBbCode within helperSnippet but got nothing so I'm not even sure where I"m messing up at.
 
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.
 
Ok Ragtek, I threw it together again, here is the error I get:
Invalid error type specified in /library/XenForo/Template/Helper/Core.php, line 95
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.
 
Back
Top Bottom