Reputation: 256901
An html template is compiled into the application as a resource. A fragment of the HTML template looks like:
<A href="%PANELLINK%" target="_blank">
<IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%">
</A><BR>
%CAPTIONTEXT%
i like it like this because the larger resource HTML file contains styling, no-quirks mode, etc.
But as is always the case, they now want the option that the Anchor tag should be omitted if there is no link. Also if there is no caption, then the BR tag should be omitted.
Given that i don't want to have to build entire HTML fragments in C# code, i considered something like:
%ANCHORSTARTTAGPREFIX%<A href="%PANELLINK%" target="_blank">%ANCHORSTARTTAGPOSTFIX%
<IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%">
%ANCHORENDTAGPREFIX%</A>%ANCHORENDTAGPOSTFIX%CAPTIONPREFIX%<BR>
%CAPTIONTEXT%%CAPTIONPOSTFIX%
with the idea that i could use the pre and postfixes to turn the HTML code into:
<!--<A href="%PANELLINK%" target="_blank">-->
<IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%">
<!--</A>--><!--<BR>
%CAPTIONTEXT%-->
But that is just rediculous, plus one answerer reminds us that it wastes bandwith, and can be buggy.
Wholesale replacement of tags:
%AnchorStartTag%
<IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%">
%AnchorEndTag%%CaptionStuff%
and doing a find-replace to change
%AnchorStartTag%
with
"<A href=\"foo\" target=\"blank\""
i considered giving an ID to the important HTML elements:
<A id="anchor" href="%PANELLINK%" target="_blank">
<IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%">
</A><BR id="captionBreak">
%CAPTIONTEXT%
and then using an HTML DOM parser to programatically delete nodes. But there is no easy access to a trustworthy HTML DOM parser. If the HTML was instead xhtml i would use various built-in/nativly available xml DOM parsers.
What i actually have so far is:
private const String htmlEmptyTemplate =
@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01//EN\"""+Environment.NewLine+
@" ""http://www.w3.org/TR/html4/strict.dtd"">"+Environment.NewLine+
@"<HTML>"+Environment.NewLine+
@"<HEAD>"+Environment.NewLine+
@" <TITLE>New Document</TITLE>"+Environment.NewLine+
@" <META http-equiv=""X-UA-Compatible"" content=""IE=edge"">"""+Environment.NewLine+
@" <META http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">"+Environment.NewLine+
@"</HEAD>"+Environment.NewLine+
@""+Environment.NewLine+
@"<BODY style=""margin: 0 auto"">"+Environment.NewLine+
@" <DIV style=""text-align:center;"">"+Environment.NewLine+
@" %ContentArea%"+Environment.NewLine+
@" </DIV>" + Environment.NewLine +
@"</BODY>" + Environment.NewLine +
@"</HTML>";
private const String htmlAnchorStartTag =
@"<A href=""%PANELLINK%"" target=""_blank"">";
//Image is forbidden from having end tag
private const String htmlImageTag =
@"<IMG border=""0"" src=""%PANELIMAGE%"" style=""%IMAGESTYLE%"">";
private const String htmlCaptionArea =
@"<BR>%CAPTIONTEXT%";
And i already want to gouge my eyeballs out. Building HTML in code is a nightmare. It's a nightmare to write, a nightmare to debug, and a nightmare to maintain - and it will makes things difficult on the next guy. i'm hoping for another solution - since i am the next guy.
Upvotes: 3
Views: 647
Reputation: 15907
Templates are a serious code smell. Take a look at how Seaside generates html.
[Edit] Another one where someone without a clue downvoted.
Upvotes: 0
Reputation: 34120
I would use Template Toolkit, unfortunately it is currently only implemented in Perl
and Python
.
test.pl
:use Template;
my $template = Template->new({
VARIABLES => {
ImageStyle => "default",
CaptionText => "Place Caption Here"
},
});
$template->process( 'test.tt', {
panel => {
link => "http://Stackoverflow.com/",
image => "/Content/Img/stackoverflow-logo-250.png",
alt => "logo link to homepage"
}
} );
test.tt
:
[% IF panel.link -%]
<A href="[% panel.link %]" alt="[% panel.alt %]" target="_blank">
[%- END -%]
[%- IF panel.image -%]
<IMG border="0" src="[% panel.image %]" style="[% ImageStyle %]">
[%- END -%]
[%- IF panel.link %]</A>[% END %]<BR>
[% CaptionText %]
<A href="http://Stackoverflow.com/" alt="logo link to homepage" target="_blank"> <IMG border="0" src="/Content/Img/stackoverflow-logo-250.png" style="default"> </A><BR> Place Caption Here
If the variable panel.link
isn't defined it skips the anchor tags.
<IMG border="0" src="/Content/Img/stackoverflow-logo-250.png" style="default"> <BR> Place Caption Here
Upvotes: 0
Reputation: 338316
What about this: Store XML as your fragment:
<fragment type="link_img_caption">
<link href="%PANELLINK%" />
<img src="%PANELIMAGE%" style="%IMAGESTYLE%" />
<caption text="%CAPTIONTEXT%" />
</fragment>
Pull it out, replace the placeholders with the "real" strings (that you have carefully XML-escaped of course),
...and use a simple XML transformation to produce HTML output:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="fragment" />
</xsl:template>
<xsl:template match="fragment[@type = 'link_img_caption']">
<xsl:choose>
<xsl:when test="link[@href != '']">
<a href="{link/@href}" target="_blank">
<img src="{img/@src}" style="{img/@style}" border="0" />
</a>
</xsl:when>
<xsl:otherwise>
<img src="{img/@src}" style="{img/@style}" border="0" />
</xsl:otherwise>
</xsl:choose>
<xsl:if test="caption[@text !='']">
<br />
<xsl:value-of select="caption/@text" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Other fragment types could be added because of the type
attribute. There is much room to improve this, so look at it as an example of how it could be done.
Output:
<a href="%PANELLINK%" target="_blank">
<img src="%PANELIMAGE%" style="%IMAGESTYLE%" border="0">
</a>
<br>
%CAPTIONTEXT%
and, if the link href is empty in the XML:
<img src="%PANELIMAGE%" style="%IMAGESTYLE%" border="0">
<br>
%CAPTIONTEXT%
Upvotes: 2
Reputation: 63606
1.) Don't use comments, you'll send useless data to the browser wasting bandwidth and encountering BUGs in IE.
2.) Would this not be better as some sort of method? (I'm not familiar with C#) but something like this makes more sense to me.
//using PHP in this example
function HTMLImage($imageData, $linkData){
var str = '';
//if there is link data, start <a> tag
$str .= '<a '.{expand any attributes}.'>';
//add image tag and attributes from $imageData
$str .= '<img '.{expand any attributes}.'/>';
//if there is link data, close <a> tag
$str .= '</a>';
return $str;
}
Upvotes: 0
Reputation: 5348
My reputation points in this game already being low gives me the freedom to tell you quite plainly that you, sir or madame, are in serious need of XSLT. Failing this (and you probably will) you need to look at XML literals in VB.NET (which provides you with the template-based solution you are looking for...). Since I prefer to stay in C# (even though I was born and raised on VBA), I use XSLT.
Both of my unwelcome recommendations require the use of XHTML instead of HTML. This requirement alone is quite a turn off to many traditional developers. I can already see through your use of capital letters for HTML elements that you will find my remarks utterly useless. So I should stop writing now.
Upvotes: 11