【问题标题】:How do I implement such template engine?如何实现这样的模板引擎?
【发布时间】:2009-10-29 15:00:04
【问题描述】:

我得到了什么:

我得到了一个文本表示,我的程序将其转换为更易读的格式,特别是对于论坛、网站等。

我为什么需要这样的模板引擎

由于有许多不同的论坛和博客,每个论坛和博客的语法可能会有所不同。我不想对不同的语法进行硬编码,而是想为每种语法生成一个类(最好可通过易于修改的 xml 文件进行扩展),以使用所需的语法格式化我的输出。

我的想象

例如我需要类似的东西

class xyz {
   private string start_bold = "[B]";
   private string end_bold = "[/B]";

   public string bold(string s) {
       return start_bold + s + end_bold;
   }
}

我怎样才能以最优雅的方式做到这一点?随意编辑这个问题,因为我不完全确定它是我需要的模板引擎。只是现在没有更好的词。

感谢您的帮助。

一些附加信息: Andrew's 答案是一个很好的提示,但我不明白我如何使用这种方法可以几种不同的风格。目前我做的很艰难:

string s = String.Format("Output of [B]{0}[b] with number [i]{1}[/i]", 
                          Data.Type,
                          Data.Number); 

对于此示例,我希望为论坛设计输出。将来我想这样做:

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]"

我只是不知道如何以最优雅的方式做到这一点。

关于 XML:xml 文档只能派生样式约定,即添加自定义样式而不使用代码。

【问题讨论】:

  • 你能给我们一个输入数据的例子,以及结果输出吗?就几行?我仍然没有看到你的问题,对不起。 . .真的很想帮忙
  • 您是否要在字符串中查找位置并将其设为粗体?例如?数据中是否存在某种一致性?你所说的“文本表示”是什么意思,我想我开始明白你的问题了。 .抱歉这么密集,现在是星期五,还有 2 个小时去周末;)
  • 它只是一些我想用几种不同的语法轻松格式化的字符串。根据我选择的样式,我想返回一个带有“[b]”标签而不是“”或“'''”的字符串。

标签: c# class templates


【解决方案1】:

我会使用扩展方法。然后你可以调用 string.bold()。

我认为这是语法:

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

见:http://msdn.microsoft.com/en-us/library/bb383977.aspx 我将下面的代码作为示例,但我认为您真正需要的是类似于“令牌系统”的东西

假设你有一个这样的字符串:

string s = "I want {~b}this text to be bold{~~b} and {~i}this text to be italics{~~i}"

您的 XML 文档应该包含这些节点(我认为,我的 xml 有点生锈)

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

            }

实施

   Layout l = new Layout("html", siteidorsomeuniqeidentifier);
   l.html = stringtoformat;
   output = l.formattedstring;

代码可以更好,但它应该让你朝着正确的方向前进 :)

编辑 2:基于进一步的信息.....

如果你想这样做:

Layout l = new Layout("html");
string s = String.Format("Output of {0} with number {1}, 
                          l.bold(Data.Type),
                          l.italic(Data.Number);

并且您希望根据博客引擎的特定标记更改l.bold()l.italic()。 . .

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

【讨论】:

  • 如何定义要使用的样式?
  • 您将拥有多个公共字符串,您将在调用 .bold() 或 .italics() 或 .hyperlink() 等之后连接字符串片段。stackoverflow.com/questions/487904/… 是正确方向的好指针.这假设您使用的是 c# 3.0 还是 3.5?我不记得是哪个了。
  • @AndrewWinn- 它是 C# 3.0。然而 .NET 框架版本是 3.5。
  • @RichardOD - 谢谢,我不记得了:)。就像我说的,只是进入扩展方法。
  • 感谢您花时间帮助我:D 我认为您最近的编辑非常适合。
猜你喜欢
  • 1970-01-01
  • 2012-01-25
  • 2015-01-21
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2011-11-16
相关资源
最近更新 更多