[Question] Pass to included template current line indentation?
Hi, I am working on templates for indentation sensitive format. It is critical that included multi line template and it’s children would keep parent template indentation. How is it possible to do in nice and easy way? Is it possible to instruct parser that would automatically insert number of spaces after “\n” sign? Thanks.
[Update] Example:
<html>
@views.Head.template(“Home”)
</html>
Head.rocker.raw
@args (String title)
<h1>@title</h1>
<h2>Loren ipsum</h2>
Expected output to be h1 and h2 had leading 4 spaces as defined in parent template.
I thought about your question and the only way to make it elegant would be to modify Rocker's parser and template generator. For example, all static text between tags/dynamic parts are grouped together and stored together. You'd probably need to split those up by line. Then use that to count the indent in the template generator and use that information in key parts to add one automatically.
If you were looking for something more quick, I'd suggest you write helper Java util's that return indented strings based on a count. Then pass the count from template to template. For example,
... in java class Util
static public String indent(int count) {
... code to create indented string
}
In template A
@args(int i) @indent(i)Your template stuff here
In template B @import static Utils.indent @args(int i) @indent(i)My template @indent(i+1)@views.A.template(i+1)
It's not elegant per-se, but should work.
On Fri, Aug 10, 2018 at 5:56 AM, Konstantin Soltsman < [email protected]> wrote:
Hi, I am working on templates for indentation sensitive format. It is critical that included multi line template and it’s children would keep parent template indentation. How is it possible to do in nice and easy way? Is it possible to instruct parser that would automatically insert number of spaces after “\n” sign? Thanks.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/fizzed/rocker/issues/87, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjwAglsbLXNFWz9hWC12_8ffrehbmCbks5uPVjXgaJpZM4V36r4 .
The proposed solution defenatly would work. I did a workaround by passing variable of ident to each template and prefix each line with @indent. But this causes PLAIN_TEXT_0_1 static string const to break into a several consts. Looks unprofessional and very dirty template. What if to implement another method for a template class, let’s say .templateIdent(..., int additionalSpaces) which instructs the parser to store it in a variable and add it to the current variable while printing PLAIN_TEXT_0_1 with spaces after \n . So performance light loss during parsing phase only as well as clean and elegant templates. Feasible or would break basic architecture?