Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Custom parser and shortcodes


Go to End


3 Posts   2184 Views

Avatar
edead

Community Member, 2 Posts

12 August 2014 at 7:17am

Edited: 12/08/2014 7:18am

Hello,
in order to maintain typographic rules for my language I have to use a custom parser for all content. The parser is simple and as such, works fine. It extends TextParser and then is called in templates with $Content.Parse(MyParser).

However, this stops shortcodes from working. Apparently using a custom parser turns off the ShortcodeParser. So how do I use a custom parser without losing shortcodes?

I searched the forums for answers, and the same question was apparently asked 3 years ago here, but was unresolved.

Avatar
martimiz

Forum Moderator, 1391 Posts

14 August 2014 at 11:47pm

This is because the HTML ShortcodeParser is invoked in the forTemplate() method of the HTMLText object.

$Content is a HTMText object so it gets shortcodeparsed.

$Content.Parse(MyParser) is whatever the MyParser::parse() function returns. So if that returns a string, nothing further will happen. Returning a HTMLText object instead will fix this:

class MyParser extends TextParser {
	
	public function parse() {

		//create some content
		$content = $this->content . "*****************";
		
		//return as a HTMLText object
		return DBField::create_field('HTMLText', $content);
		
	}
}

Avatar
edead

Community Member, 2 Posts

25 August 2014 at 12:02pm

A late but due thanks, martimiz, this worked perfectly.