Jump to:

3447 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Custom parser and shortcodes

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

Page: 1
Go to End
Author Topic: 119 Views
  • edead
    Avatar
    Community Member
    2 Posts

    Custom parser and shortcodes Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: Custom parser and shortcodes Link to this post

    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);
          
       }
    }

  • edead
    Avatar
    Community Member
    2 Posts

    Re: Custom parser and shortcodes Link to this post

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

    119 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.