howto use custom bbcode in listener with new bbcode engine

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
DevMakoto
Registered User
Posts: 65
Joined: Tue Jun 23, 2015 3:38 pm
Name: Carlos Mayorga
Contact:

howto use custom bbcode in listener with new bbcode engine

Post by DevMakoto » Sat Jan 14, 2017 3:10 am

My custom bbcode

Code: Select all

[student]{NUMBER}[/student]
replace bbcode (in dude)

Code: Select all

<div style="display: none;">{NUMBER}</div>
howto parse bbcode in function (example)

Code: Select all

public function student_template($number)
{
	$student = new Student($number);

	$html = ''';
	
	$html .= '<div class="fullname" ><h3>'. $student->getFullName .'</h3>';
	$html .= '<div class="photo" ><img src="'. $student->getPhoto() .'" /></div>';
	.
	.
	.
	return $html;
}
As it would be in the listener?

in posting

Code: Select all

[student]1234567[/student]
in preview

Code: Select all

<html template>
in viewtopic

Code: Select all

<html template>
in database (*optional)

Code: Select all

[student]1234567[/student]
i hope you understand me

*sorry no speak english

User avatar
JoshyPHP
Code Contributor
Posts: 998
Joined: Mon Jul 11, 2011 12:28 am

Re: howto use custom bbcode in listener with new bbcode engine

Post by JoshyPHP » Sat Jan 14, 2017 11:26 am

In your case I think it's better to have a custom BBCode with a simple template and then replace it in PHP.

BBCode template:

Code: Select all

<div class="student">{NUMBER}</div>
Listener:

Code: Select all

class listener implements EventSubscriberInterface
{
	public static function getSubscribedEvents()
	{
		return array('core.text_formatter_s9e_render_after' => 'replace_student');
	}

	public function replace_student($event)
	{
		$event['html'] = preg_replace_callback(
			'(<div class="student">(\\d+)</div>)',
			function ($m)
			{
				$student = new Student($m[1]);
				$html    = '...';

				return $html;
			},
			$event['html']
		);
	}
}
I wrote the thing that does BBCodes in 3.2.

User avatar
DevMakoto
Registered User
Posts: 65
Joined: Tue Jun 23, 2015 3:38 pm
Name: Carlos Mayorga
Contact:

Re: howto use custom bbcode in listener with new bbcode engine

Post by DevMakoto » Sat Jan 14, 2017 5:03 pm

very thanks :D

Post Reply

Return to “Extension Writers Discussion”