views:

932

answers:

5

Is it possible to use the asp templating engine (With the partial code-behind class, dynamic <% ... %> blocks and such) to generate non HTML? I want to have a clean and maintainable way to generate code dynamically. (Specifically, I want to generate LaTeX code populated with values from a database.)

Currently my LaTeX templates are resource strings with placeholders that I string.replace with the database values. This solution rapidly became very difficult to maintain and clean. I'd really like to use the dynamic blocks from aspx markup, but I'm not sure a) whether aspx will throw a fit when the output isn't HTML, or b) how to actually render the result into a .tex file.

The generating code itself is located in a C# .dll. We're using .NET 3.5

Is this possible? Thanks in advance.

A: 

I don't see why not. Someone I knew at a former job created a database wrapper generator using ASP.NET pages and the repeater control to insert properties. He then wrote out the document contents to a source file.

If you are worried about ASP.NET will throw a fit, you can just create a very limited test case and see for yourself. Shouldn't take much time to test a theory and let you know if it meets your needs.

Jason Z
A: 

It's certainly possible. Most server controls will be out, as they'll automatically emit HTML markup. But, you can databind the page and use databinding expressions. Visual Studio will no doubt complain about invalid markup.

You then have to run your pages through Cassini or the ASP.NET pipeline to get the output. I've got a unit test harness somewhere that does that, and it's surprisingly easy.

A better idea, though, would probably be to use a code generator. Something like CodeSmith should work nicely, or even Visual Studio's built in T4 gives you a lot of flexibility while not trying to tie you into HTML.

Mark Brackett
But can T4 templates be rendered at runtime?
Wyatt
A: 

For code generation you should take a look into the T4 templating features. It uses a syntax similar to ASP.Net.

See Scott Hanselmans Post: http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

Brian Schmitt
A: 

Yes you can. Just create a standard .aspx page, delete all the HTML and place whatever content you want in the page. Then you can use <% %> tags to place dynamic content within the page. And like Jason Z said, you can use the Repeater control to iterate through collections of items to list out in the "page". Also, you wont be able to use all the other server controls since they generate HTML, but you can still create your own server control and/or user controls as necessary.

Chris Pietschmann
+7  A: 

The T4 templating that comes with Visual Studio 2008 natively or with Visual Studio 2005 SDK, you can pretty much generate anything you want.

You can have more info on the following links:

I'm pretty sure that all those links is a good start to your quest.

If you want to generate T4 templates outside of Visual Studio, there is custom MSBuild task to invoke a T4 template (link)

Here is a sample of the "Execute" code of the MSBuild task. Click here for the source code:

public override bool Execute()
{
    bool success = false;

    //read in the template:
    string template = File.ReadAllText(this.TemplatePath);

    //replace tags with property and item group values:
    ProjectHelper helper = new ProjectHelper(this);
    template = helper.ResolveProjectItems(template);

    //copy the template to a temp file:
    this._tempFilePath = Path.GetTempFileName();
    File.WriteAllText(this._tempFilePath, template);

    //shell out to the exe:
    ProcessHelper.Run(this, TextTransform.ToolPath, TextTransform.ExeName, string.Format(TextTransform.ArgumentFormat, this.OutputPath, this._tempFilePath));
    success = true;

    return success;
}
Maxim
From my understanding T4 is for design time generation. I need to generate the latex at runtime, sortof like an asp. If T4 can be called at runtime it looks like the right tool, but I'm not sure it can.
Wyatt
Just edited that and foundd you a link to generate it from MSBuild instead of Visual Studio.
Maxim
Brilliant! Thanks.
Wyatt
T4 is part of Visual Studio, so you can't distribute it with your app to use at Runtime.
Chris Pietschmann