On Fri, 09 Aug 2019 03:57:24 GMT, Carl Sturtivant wrote:

On Thu, 08 Aug 2019 16:37:44 GMT, Carl Sturtivant wrote:

Hello,

Is there an alternative to render that simply returns the HTML as a string instead of sending it to an HTTPServerResponse?

I managed to make this work using the template compileHTMLDietString though the technique is not as clean as I would like. I was able to use a Traits struct to pass as a type to that template to generate pretty HTML, but indentation collapses of course as soon as I embed it inside a diet template.

Is there a cleaner way to proceed? I want to produce pretty (nested with nice indentation) HTML.

html
    body
        - import diet.html : compileHTMLDietFile;
        - _diet_output.compileHTMLDietFile!("nested.dt", some_param);

But that doesn't solve the indentation issue. As far as I can see, the only solution to this right now is to use an appender!string as the destination for compileHTMLDiet(File/String) and then do .lineSplitter.map!(ln => "\t".replicate(depth) ~ ln).join to manually correct the nesting.

The next best step would then be to either introduce a new base indentation trait, or an additional argument to compileHTMLDiet.... To avoid having to manually determine the required nesting level, a variable similar to _diet_output could be introduced (_diet_output_nesting_level).

Then the only question that remains is how to encapsulate this into a more friendly API. It would be possible to define nested template functions within compileHTMLDietFile (e.g. compileNestedHTML(File/String)) that automatically use the those two variables appropriately and also forward the traits properly, I'm just not sure yet whether that would be a desirable API architecture.

BTW, a totally different possible approach to this is to use include and/or a function to generate the HTML:

html
    body
        include nested
        - renderNested(some_param);

nested.dt:

- void renderNested(string some_param)
    p Hello, World: #{some_param}

This should properly propagate the nesting level, and by using the function definition around the HTML, it also allows to render the contents multiple times with different runtime parameters.