Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using aspx Rendering Engine to Generate Non-HTML

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.

like image 434
Wyatt Avatar asked Oct 22 '08 19:10

Wyatt


1 Answers

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:

  • Scott Hanselman's Blog
  • Rob Conery's Blog
  • Oleg Sych's Blog which is an entire repository of T4 examples
  • Johan Danforth's Blog

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;
}
like image 81
Maxime Rouiller Avatar answered Nov 02 '22 04:11

Maxime Rouiller