Skip to content

[Question] How to output a form feed character in a template #593

@klreeher

Description

@klreeher

I have a handlebars template that looks like so:

{{#each thing}}
... bunch of straight forward templating ...
{{/each}}
{{{formFeed}}}

The form feed character is \f or https://unicodeplus.com/U+000C
I need my list of things to be output with a form feed character after each thing gets templated. I've tried a couple different things, including:

template:

{{#each thing}}
... bunch of straight forward templating ...
{{/each}}
{{{\f}}}

None of these, when I write the compiled text to file, actually have the form feed character.

I've tried tackling this through the writing to file side, thinking it was something there, but if I only template the thing section and write the form feed character like this, it works:

            using (StreamWriter sw = new StreamWriter(File.Open(outputFile, FileMode.CreateNew), Encoding.Unicode))
            {
                foreach (var thing in thingData)
                {
                    var text = RenderThing(ticket); // this is the handlebars.compile(template + data)
                    sw.WriteLine(text);
                    sw.WriteLine("\f");
                }

            }

This is not an ideal solution because one of the values the template uses for the looped things is the index of the thing in the list, but I can't figure out how to get the template to actually include the real form feed character.

If I pass the formfeed character in with the template data, like this:

        public string RenderThing(List<ThingObject> thingData)
        {
            string compileText = "{{> " + _templateName + "}}";
            _patternTemplate ??= _handlebars.Compile(compileText);

            var data = new
            {
                tickets = thingData,
                formFeed = "\f"
            };

            var text = _patternTemplate(data);
            return text;
        }

and I have the {{{formFeed}}} outside the #each loop, then I can get the form feed character to show up at the end. But if I try to move it into the loop, it's not rendered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions