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.

General Questions /

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

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

Pass tag name to custom text parser


Go to End


2 Posts   2153 Views

Avatar
DeklinKelly

Community Member, 197 Posts

12 May 2009 at 5:17am

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.'>';
   }
}

?>

Avatar
bummzack

Community Member, 904 Posts

12 May 2009 at 5:41am

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.