Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Pass tag name to custom text parser

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1258 Views
  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Pass tag name to custom text parser Link to this post

    I want to pass tag names to a Text Parser.

    I want to place something like this in my ss template:
    $Text2.Parse(TextToXHTML(p))
    or
    $Text2.Parse(TextToXHTML.h1)

    But I don't know how to pass on the tag name.

    <?php

    class TextToXHTML extends TextParser
    {
    function parse($tag){
    $str = $this->content;
    $str = htmlentities($str);
    $str = str_ireplace(array('&quot;','&#34','&#x22;'),'"',$str);
    $str = str_ireplace(array('&lsquo;','&#8216;','&#x2018;','&rsquo;','&#8217;','&#x2019;','&#039;','&#x27;','&#x0027;'),"'",$str);
    $str = str_replace("'",'&lsquo;',preg_replace('/([A-Z0-9])\'/i','$1&rsquo;',$str));
    $str = str_replace('"','&ldquo;',preg_replace('/([A-Z0-9])\"/i','$1&rdquo;',$str));
    $str = str_replace("\n\n",'</'.$tag.'><'.$tag.'>', $str);
    $str = str_replace("\n","<br />\n", $str);
    return '<'.$tag.'>' . $str . '</'.$tag.'>';
    }
    }

    ?>

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Pass tag name to custom text parser Link to this post

    Hi hknight

    I'm afraid this won't work. You can't pass constructor arguments to the TextParser subclasses via templates.
    It works (roughly) like this:
    1) Template parser finds .Parse(xy) statement
    2) It checks if xy is a class and if it's a subclass of TextParser
    3) It creates a new instance of xy, passing the content to the constructor
    4) The xy->parse() return value is being returned.

    So there's actually no straightforward way to inject your parameters in this process.
    You could do the following however:

    <?php
    class TextToXHTML extends TextParser
    {
       private static $tag = 'div';
       
       function parse(){
          $str = $this->content;
          $str = htmlentities($str);
          $str = str_ireplace(array('&quot;','&#34','&#x22;'),'"',$str);
          $str = str_ireplace(array('&lsquo;','&#8216;','&#x2018;','&rsquo;','&#8217;','&#x2019;','&#039;','&#x27;','&#x0027;'),"'",$str);
          $str = str_replace("'",'&lsquo;',preg_replace('/([A-Z0-9])\'/i','$1&rsquo;',$str));
          $str = str_replace('"','&ldquo;',preg_replace('/([A-Z0-9])\"/i','$1&rdquo;',$str));
          $str = str_replace("\n\n",'</'.self::$tag.'><'.self::$tag.'>', $str);
          $str = str_replace("\n","<br />\n", $str);
          return '<'.self::$tag.'>' . $str . '</'.self::$tag.'>';
       }
       
       static function setTag($tag = 'div'){
          self::$tag = $tag;
       }
    }

    Then you would need a helper method to alter the Tag in your Page.php class. Something like this:

    // put this in Page_Controller
    public function SetTag($tag){
       TextToXHTML::setTag($tag);
    }

    Then you would be able to do something like this in your template:

    $SetTag(p)
    $Text2.Parse(TextToXHTML)

    All subsequent calls after the SetTag call will use <p> as tag...
    It's not a very elegant solution, but maybe the one with the least work involved. Maybe somebody here has got a better idea.

    On a side note: What are you actually trying to achieve? Did you have a look at the methods of the Convert class (sapphire/core/Convert.php)? There are quite a lot of helper functions to convert from RAW text to XML/XHTML and vice-versa.

    1258 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.