21294 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1095 Views |
-
Pass tag name to custom text parser

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('"','"','"'),'"',$str);
$str = str_ireplace(array('‘','‘','‘','’','’','’',''',''','''),"'",$str);
$str = str_replace("'",'‘',preg_replace('/([A-Z0-9])\'/i','$1’',$str));
$str = str_replace('"','“',preg_replace('/([A-Z0-9])\"/i','$1”',$str));
$str = str_replace("\n\n",'</'.$tag.'><'.$tag.'>', $str);
$str = str_replace("\n","<br />\n", $str);
return '<'.$tag.'>' . $str . '</'.$tag.'>';
}
}?>
-
Re: Pass tag name to custom text parser

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('"','"','"'),'"',$str);
$str = str_ireplace(array('‘','‘','‘','’','’','’',''',''','''),"'",$str);
$str = str_replace("'",'‘',preg_replace('/([A-Z0-9])\'/i','$1’',$str));
$str = str_replace('"','“',preg_replace('/([A-Z0-9])\"/i','$1”',$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.
| 1095 Views | ||
|
Page:
1
|
Go to Top |


