0

I want to be able to achieve code templating in ASP.NET, with the power of PHP. The following two code files result in the same output, but the PHP one is more elegant, and can even be imported into other PHP files so it can be reused.

ASP.NET:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ASP_dot_NET_test.WebForm1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server"><title></title></head>
<body>
<script runat="server">
    delegate void Make_buttons(int count);
</script>
<%
Make_buttons Make_buttons = i =>
    {
    for (int k = 0; k < 2; k++)
        {
        %>
        <hr />
        <button><%= "buy " + i + " candies!" %></button>
        <%
        }
    };
%>

<div>
    <%
    Make_buttons(count: 5);
    %>
</div>
</body>
</html>

PHP:

<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<?php
function Make_buttons ($i)
    {
    for ($k = 0; $k < 2; $k++)
        {
        ?>
        <hr />
        <button><?= "buy " . $i . " candies!"; ?></button>
        <?php
        }
    }
?>

<div>
    <?php
    Make_buttons(5);
    ?>
</div>
</body>
</html>

I know about Master Pages, but they accomplish page templating, not code templating like above.

What are some of the best ways I can accomplish this?

daniel1426
  • 169
  • 3
  • 13
  • Not exactly sure what are you looking for, but maybe code behind or user controls or Razor partial views works? – Alexei Levenkov Nov 02 '13 at 17:38
  • Not code-behind, since it can't have HTML literals. Also not user controls, because I don't intend to create controls. I want the HTML-preprocessing power and convenience of PHP, in ASP.NET. And no, using PHP is not an option. – daniel1426 Nov 02 '13 at 17:48
  • I think you should check out [Razor](http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx)... On your other comments: "code-behind ... can't have HTML literals" - clearly not true since ASPX pages first converted to C# code/compiled and than rendered; [user controls](http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.90).aspx) are just reusable pieces of pages, not "controls" in seance of "a button". – Alexei Levenkov Nov 02 '13 at 18:08
  • can you show me an example of code-behind that has HTML literals? – daniel1426 Nov 02 '13 at 19:43
  • added sample as "answer" because it can't fit into comment. – Alexei Levenkov Nov 03 '13 at 01:44

2 Answers2

0

Samples (not an answer):

Writing literal text to output with Controls using Control.RenderControl:

protected override void Render(HtmlTextWriter output)
{       
   output.Write("<br>Message from Control : " + Message);       
   output.Write("Showing Custom controls created in reverse" +
                                                    "order");         
}

Auto-generated code from ASPX:

<form id="form1" runat="server">
<div>
    Some text <%=42%> more text.

</div>
</form>

results in:

 private void @__Renderform1(
     System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) 
 {
        @__w.Write("\r\n    <div>\r\n        Some text ");
      @__w.Write(42);
       @__w.Write(" more text.\r\n\r\n    </div>\r\n    ");
 }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

I don't know how to do that in .aspx. If you are willing to switch to Razor templates...You can use helper syntax, as in...

@helper SayHello(string name)
{
    <div>Hello @name</div>
}

@SayHello("John")

You can share a helper across multiple files by making the magic directory "App_Code" and placing your shared cshtml files in there.

http://www.asp.net/web-pages/overview/ui-layouts-and-themes/creating-and-using-a-helper-in-an-aspnet-web-pages-site

Move common razor helpers to another file

How do I define a method in Razor?

Community
  • 1
  • 1
David Jeske
  • 2,306
  • 24
  • 29