实现生成text / html格式输出的函数的优雅方法是什么?

时间:2022-05-11 22:28:49

My function parses through text, grabs parts and generates text only email format. But it also needs to generate html format.

我的函数解析文本,抓取部分并生成仅文本的电子邮件格式。但它也需要生成html格式。

The brain-dead way would be to use if ... else ... and add additional html tags around each paragraph or element. But it will violate DRY (don't repeat yourself) rule.

脑死亡的方法是使用if ... else ...并在每个段落或元素周围添加额外的html标签。但它会违反DRY(不要重复自己)规则。

Is there an elegant way to solve this problem?

有没有一种优雅的方法来解决这个问题?

2 个解决方案

#1


2  

As @Pointy said, you should look into templates. jQuery templates, while in beta, are good enough - although there are lots of alternatives for good javascript templates.

正如@Pointy所说,你应该看一下模板。 jQuery模板虽然处于测试阶段,但已经足够好了 - 尽管有很多替代方案可用于良好的javascript模板。

In your case, you would do something like:

在你的情况下,你会做类似的事情:

$.template("textTemplate", "Hi ${name}!\n\nWelcome as a member!");
$.template("htmlTemplate", "<h1>Hi ${name}!</h1><p>Welcome as a member!</p>");

And then use them like this:

然后像这样使用它们:

var emailText = $.tmpl("textTemplate", data);

// Show the html
$.tmpl("htmlTemplate", data).appendTo("#container");

#2


0  

You could use a series of regular expressions to fill in html tags. But be warned that there are lots of warnings out there to not use RegEx for parsing html: RegEx match open tags except XHTML self-contained tags

您可以使用一系列正则表达式来填充html标记。但是请注意,有很多警告不使用RegEx来解析html:RegEx匹配开放标签,除了XHTML自包含标签

I would implement the Regular expressions like so:

我会像这样实现正则表达式:

textVAR=YourTextInput;
htmlVAR=textVAR.replace(Reg Ex, "New html tags");// run through a series of RegEx
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");// fill in html tags where needed
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");// be sure to add "/g" to your RegEx to make it global
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");
alert(htmlVAR);

More resources: Finding URLs is difficult: http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html

更多资源:查找URL很困难:http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html

Consider using PHP - here is a good resource for finding line breaks: http://us2.php.net/nl2br

考虑使用PHP - 这是查找换行符的好资源:http://us2.php.net/nl2br

#1


2  

As @Pointy said, you should look into templates. jQuery templates, while in beta, are good enough - although there are lots of alternatives for good javascript templates.

正如@Pointy所说,你应该看一下模板。 jQuery模板虽然处于测试阶段,但已经足够好了 - 尽管有很多替代方案可用于良好的javascript模板。

In your case, you would do something like:

在你的情况下,你会做类似的事情:

$.template("textTemplate", "Hi ${name}!\n\nWelcome as a member!");
$.template("htmlTemplate", "<h1>Hi ${name}!</h1><p>Welcome as a member!</p>");

And then use them like this:

然后像这样使用它们:

var emailText = $.tmpl("textTemplate", data);

// Show the html
$.tmpl("htmlTemplate", data).appendTo("#container");

#2


0  

You could use a series of regular expressions to fill in html tags. But be warned that there are lots of warnings out there to not use RegEx for parsing html: RegEx match open tags except XHTML self-contained tags

您可以使用一系列正则表达式来填充html标记。但是请注意,有很多警告不使用RegEx来解析html:RegEx匹配开放标签,除了XHTML自包含标签

I would implement the Regular expressions like so:

我会像这样实现正则表达式:

textVAR=YourTextInput;
htmlVAR=textVAR.replace(Reg Ex, "New html tags");// run through a series of RegEx
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");// fill in html tags where needed
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");// be sure to add "/g" to your RegEx to make it global
htmlVAR=htmlVAR.replace(Reg Ex, "New html tags");
alert(htmlVAR);

More resources: Finding URLs is difficult: http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html

更多资源:查找URL很困难:http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html

Consider using PHP - here is a good resource for finding line breaks: http://us2.php.net/nl2br

考虑使用PHP - 这是查找换行符的好资源:http://us2.php.net/nl2br