tdammers / ginger

A Haskell implementation of the Jinja template language.
MIT License
77 stars 13 forks source link

a way to display a structured variable #59

Open plsnp opened 4 years ago

plsnp commented 4 years ago

When i have a nest structure of arrays and objects and do {{var}} it concatenates all values. Instead could it print a JSON like format to see the structure? If this is not desirable with the plain {{}} maybe a function like twig's dump could be added.

tdammers commented 4 years ago

This is easily possible; for example, sprinkles provides json() and yaml() as functions / filters in its default template context.

However, I did't want to include such a function in the core ginger library, for three reasons: first, because the exact formatting you want will differ between applications, and whatever default we offer is likely going to be not quite what you want; second, because it would increase code size, dependency footprint, etc., for questionable gains; and third, because the structure of GVal is such that it's not always obvious what the dump should look like. In PHP, a value can be a sorted-hashmap-from-hell ("array" in PHP terminology), a byte array (confusingly called "string" in PHP), a number, etc., but never more than one of them - but a GVal may or may not offer conversions to any number of these, and it's not automatically clear which one you want.

In other words, if asDict is Just, then that doesn't mean that the value is a dictionary; it just means that a meaningful conversion to or representation as a dictionary exists. But there may also be a meaningful HTML representation, textual representation, list representation and/or numeric representation for such a value - and there is no general mechanism to tell which one you wanted.

plsnp commented 4 years ago

Maybe there can be a dump() function that prints something as a haskell value / type that indicates that it can have multiple representations?

For my use case though json() seems to have already enough debugging capabilities so i'm happy with that :) I see it's listed here https://ginger.tobiasdammers.nl/guide/syntax/filters/ which confuses me a bit since you say it's in sprinkles ??