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.

Template Questions /

Custom parser and shortcodes


Reply


3 Posts   274 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 [url=http://www.silverstripe.org/general-questions/show/17304]here[/url], but was unresolved.

Avatar
martimiz

Forum Moderator, 1132 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.