Using T4 templates to server html pages with javascript

761 views Asked by At

i am new to T4 templates and i was thinking how can create and serve generated html pages through T4.

I have this part of code in the test1.tt file

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<div id='test'></div>
<script type='text/javascript'>
    function insertText () {
        document.getElementById('test').innerHTML = 'Some text here';
    }
</script>

And i have this c# code that generate it

    [HttpGet]
    public string Generate([FromBody]dynamic value)
    {
        try
        {         
            string webResponseText = new test1().TransformText();
            return webResponseText;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

A similar logic example that i want to make is something like "You create a program for a friend with all files, but if you want your friend run this program you will give him the .exe the compiled file from your files, you don't tell him take all files, compiled them and next run the .exe"

something like that but now my friend is the browser.

So, is possible to run this .tt file, run all javascript code inside and take the generated output?

I have a Asp.Net core project, because T4 is not fully supported in core i have this Action that call a other web api that make all 'job'

[Produces("text/html")]
[HttpGet]
public async Task<String> T4test()
{
    using (var client = new HttpClient())
    {
        try
        {
            client.BaseAddress = new Uri(WebAPI);
            var response = await client.GetAsync("Generate");
            response.EnsureSuccessStatusCode();

            string res = await response.Content.ReadAsStringAsync();
            res = res.Trim('"');
            return res;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

I run localhost:12895/Entities/T4test this action make a call to other function 'Generate' that run .tt file. The result i get is a empty html page, using f12 i can see that javascript code exist but never called, dont change the text of element.

So, my question is simple, can use T4 to create a html page that run javascript file and return it to my Core action? This type is a string? Is possible to create a compiled file and give it ready to browser?

0

There are 0 answers