What I got:
I got a textual representation which my program converts to a much more readable format, especcially for forums, websites and 开发者_高级运维so on.
Why do I need such templage engine
As there are many different forums and blogs, the syntax of each one might be different. Instead of hard-coding the different syntax I would like to generate one class for each of those syntax (preferable extandable with easy modified xml-files) to format my output with the desired syntax.
What I did imagine
For example I need something like
class xyz {
private string start_bold = "[B]";
private string end_bold = "[/B]";
public string bold(string s) {
return start_bold + s + end_bold;
}
}
How can I do that the most elegant way? Feel free to edit this question as I'm not entirely sure it's a template engine I need. Just don't got a better word for it now.
Thanks for any help.
Some additional information: Andrew's answer was a great hint, but I don't understand how I could several different styles with this method. Currently I do it the hard way:
string s = String.Format("Output of [B]{0}[b] with number [i]{1}[/i]",
Data.Type,
Data.Number);
For this example, I want the output to be designed for a forum. In future I would like to do it like this:
Layout l = new Layout("html");
string s = String.Format("Output of {0} with number {1},
l.bold(Data.Type),
l.italic(Data.Number);
//desired output if style "html" is chosen: "Output of <b>Name</b> with number <i>5</i>" //desired output if style "phpbb" is chosen: "Output of [b]Name[/b] with number [i]5[/i]"
I just don't know how this can be done in the most elegant way.
About the XML: Only the styling conventions should be derived by a xml-document, i.e. adding custom styles without using code.
I would use exension metods. Then you could call string.bold().
I think this would be the syntax:
class xyz {
private string start_bold = "[B]";
private string end_bold = "[/B]";
public static string bold(this string x) {
return start_bold + x + end_bold;
}
}
See: http://msdn.microsoft.com/en-us/library/bb383977.aspx I'm leaving the code below as an example, but I think what you really need is something along the lines of a "token system"
Say you have a string as such:
string s = "I want {~b}this text to be bold{~~b} and {~i}this text to be italics{~~i}"
You XML document should contain these nodes (i think, my xml is kinda rusty)
<site>
<html>
<style value="{~b}">[b]</style>
<style value="{~~b}">[/b]</style>
<style value="{~i}">[i]</style>
<style value="{~~i}">[/i]</style>
</html>
<phpBBCode>
......
public class Layout {
//private string start_bold = "[B]";
//private string end_bold = "[/B]";
//private string start_italics = "[I]";
//private string end_italics = "[/I]";
private string _stringtoformat;
public string StringToFormat {set{ _stringtoformat = value;}};//syntax is wrong
private string _formattedString;
public string FormattedString {get return _formattedString;}
public Layout(string formattype, int siteid)
{
//get format type logic here
//if(formattype.ToLower() =="html")
//{ . . . do something . . . }
//call XML Doc for specific site, based upon formattype
if(!String.IsNullorEmpty(_stringtoformat))
{
//you will want to put another loop here to loop over all of the custom styles
foreach(node n in siteNode)
{
_stringtoformat.Replace(n.value, n.text);
}
}
//Sorry, can't write XML document parsing code off the top of my head
_formattedString = _stringtoformat;
}
public string bold(this string x) {
return start_bold + x + end_bold;
}
public string italics(this string x) {
return start_italics + x+ end_italics;
}
}
IMPLEMENTATION
Layout l = new Layout("html", siteidorsomeuniqeidentifier);
l.html = stringtoformat;
output = l.formattedstring;
The code can be better, but it should give you a kick in the right direction :)
EDIT 2: based upon further info.....
If you want to do this:
Layout l = new Layout("html");
string s = String.Format("Output of {0} with number {1},
l.bold(Data.Type),
l.italic(Data.Number);
and you are looking to change l.bold()
and l.italic()
based upon the blog engines specific mark up . . .
public class Layout {
private string start_bold = "[B]";
private string end_bold = "[/B]";
private string start_italics = "[I]";
private string end_italics = "[/I]";
public Layout(string formattype, int siteid)
{
//get format type logic here
//if(formattype.ToLower() =="html")
//{ . . . do something . . . }
//call XML Doc for specific site, based upon formattype
start_bold = Value.From.XML["bold_start"];
end_bold = Value.From.XML["bold_end"];
//Sorry, can't write XML document parsing code off the top of my head
}
public string bold(this string x) {
return start_bold + x + end_bold;
}
public string italics(this string x) {
return start_italics + x+ end_italics;
}
}
Layout l = new Layout("html", siteid);
string s = String.Format("Output of {0} with number {1},
ValueToBeBoldAsAstring.bold(),
ValueToBeItalicAsAstring.italic());
精彩评论