mustache-2.4.3.1: A mustache template parser library.

Copyright | (c) Justus Adam 2015
License | BSD3
Maintainer | dev@justus.science
Stability | experimental
Portability | POSIX
Safe Haskell | None
Language | Haskell2010

Text.Mustache.Render

Contents

- Substitution
- Checked substitution
- Working with Context
- Util
- Orphan instances

Description

Synopsis

- substitute :: ToMustache k => Template -> k -> Text
- substituteValue :: Template -> Value -> Text
- checkedSubstitute :: ToMustache k => Template -> k -> ([SubstitutionError], Text)
- checkedSubstituteValue :: Template -> Value -> ([SubstitutionError], Text)
- data SubstitutionError
  - = VariableNotFound [Key]
  - | InvalidImplicitSectionContextType String
  - | InvertedImplicitSection
  - | SectionTargetNotFound [Key]
  - | PartialNotFound FilePath
  - | DirectlyRenderedValue Value
- data Context α = Context {
  - ctxtParents :: [α]
  - ctxtFocus :: α
  }
- search :: [Key] -> SubM (Maybe Value)
- innerSearch :: [Key] -> Value -> Maybe Value
- data SubM a
- substituteNode :: Node Text -> SubM ()
- substituteAST :: STree -> SubM ()
- catchSubstitute :: SubM a -> SubM (a, Text)
- toString :: Value -> SubM Text

# Substitution

substitute :: ToMustache k => Template -> k -> Text Source #

Substitutes all mustache defined tokens (or tags) for values found in the provided data structure.

Equivalent to `substituteValue . toMustache`.

substituteValue :: Template -> Value -> Text Source #

Substitutes all mustache defined tokens (or tags) for values found in the provided data structure.

# Checked substitution

checkedSubstitute :: ToMustache k => Template -> k -> ([SubstitutionError], Text) Source #

Substitutes all mustache defined tokens (or tags) for values found in the provided data structure and report any errors and warnings encountered during substitution.

This function always produces results, as in a fully substituted/rendered template, it never halts on errors. It simply reports them in the first part of the tuple. Sites with errors are usually substituted with empty string.

The second value in the tuple is a template rendered with errors ignored. Therefore if you must enforce that there were no errors during substitution you must check that the error list in the first tuple value is empty.

Equivalent to `checkedSubstituteValue . toMustache`.

checkedSubstituteValue :: Template -> Value -> ([SubstitutionError], Text) Source #

Substitutes all mustache defined tokens (or tags) for values found in the provided data structure and report any errors and warnings encountered during substitution.

This function always produces results, as in a fully substituted/rendered template, it never halts on errors. It simply reports them in the first part of the tuple. Sites with errors are usually substituted with empty string.

The second value in the tuple is a template rendered with errors ignored. Therefore if you must enforce that there were no errors during substitution you must check that the error list in the first tuple value is empty.

data SubstitutionError Source #

Type of errors we may encounter during substitution.

Constructors

VariableNotFound [Key] | The template contained a variable for which there was no data counterpart in the current context
InvalidImplicitSectionContextType String | When substituting an implicit section the current context had an unsubstitutable type
InvertedImplicitSection | Inverted implicit sections should never occur
SectionTargetNotFound [Key] | The template contained a section for which there was no data counterpart in the current context
PartialNotFound FilePath | The template contained a partial for which there was no data counterpart in the current context
DirectlyRenderedValue Value | A complex value such as an Object or Array was directly rendered into the template (warning)

#### Instances

Instances details

Show SubstitutionError Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods showsPrec :: Int -> SubstitutionError -> ShowS Source # show :: SubstitutionError -> String Source # showList :: [SubstitutionError] -> ShowS Source #

# Working with Context

data Context α Source #

Representation of stateful context for the substitution process

Constructors

Context |

Fields

- ctxtParents :: [α]
- ctxtFocus :: α

#### Instances

Instances details

Show α => Show (Context α) Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods showsPrec :: Int -> Context α -> ShowS Source # show :: Context α -> String Source # showList :: [Context α] -> ShowS Source #
Eq α => Eq (Context α) Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods (==) :: Context α -> Context α -> Bool Source # (/=) :: Context α -> Context α -> Bool Source #
Ord α => Ord (Context α) Source # |

Instance details

Defined in Text.Mustache.Internal.Types

Methods

compare :: Context α -> Context α -> Ordering Source #

(<) :: Context α -> Context α -> Bool Source #

(<=) :: Context α -> Context α -> Bool Source #

(>) :: Context α -> Context α -> Bool Source #

(>=) :: Context α -> Context α -> Bool Source #

max :: Context α -> Context α -> Context α Source #

min :: Context α -> Context α -> Context α Source #

ToMustache (Context Value -> STree -> STree) Source # |
Instance details Defined in Text.Mustache.Render Methods toMustache :: (Context Value -> STree -> STree) -> Value Source # listToMustache :: [Context Value -> STree -> STree] -> Value
ToMustache (Context Value -> STree -> Text) Source # |
Instance details Defined in Text.Mustache.Render Methods toMustache :: (Context Value -> STree -> Text) -> Value Source # listToMustache :: [Context Value -> STree -> Text] -> Value
ToMustache (Context Value -> STree -> Text) Source # |
Instance details Defined in Text.Mustache.Render Methods toMustache :: (Context Value -> STree -> Text) -> Value Source # listToMustache :: [Context Value -> STree -> Text] -> Value
ToMustache (Context Value -> STree -> String) Source # |
Instance details Defined in Text.Mustache.Render Methods toMustache :: (Context Value -> STree -> String) -> Value Source # listToMustache :: [Context Value -> STree -> String] -> Value
MonadReader (Context Value, TemplateCache) SubM Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods ask :: SubM (Context Value, TemplateCache) Source # local :: ((Context Value, TemplateCache) -> (Context Value, TemplateCache)) -> SubM a -> SubM a Source # reader :: ((Context Value, TemplateCache) -> a) -> SubM a Source #

search :: [Key] -> SubM (Maybe Value) Source #

Search for a key in the current context.

The search is conducted inside out mening the current focus is searched first. If the key is not found the outer scopes are recursively searched until the key is found, then `innerSearch` is called on the result.

innerSearch :: [Key] -> Value -> Maybe Value Source #

Searches nested scopes navigating inward. Fails if it encunters something other than an object before the key is expended.

data SubM a Source #

#### Instances

Instances details

Applicative SubM Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods pure :: a -> SubM a Source # (<*>) :: SubM (a -> b) -> SubM a -> SubM b Source # liftA2 :: (a -> b -> c) -> SubM a -> SubM b -> SubM c Source # (*>) :: SubM a -> SubM b -> SubM b Source # (<*) :: SubM a -> SubM b -> SubM a Source #
Functor SubM Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods fmap :: (a -> b) -> SubM a -> SubM b Source # (<$) :: a -> SubM b -> SubM a Source #
Monad SubM Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods (>>=) :: SubM a -> (a -> SubM b) -> SubM b Source # (>>) :: SubM a -> SubM b -> SubM b Source # return :: a -> SubM a Source #
ToMustache (STree -> SubM STree) Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods toMustache :: (STree -> SubM STree) -> Value Source # listToMustache :: [STree -> SubM STree] -> Value
ToMustache (STree -> SubM Text) Source # |
Instance details Defined in Text.Mustache.Render Methods toMustache :: (STree -> SubM Text) -> Value Source # listToMustache :: [STree -> SubM Text] -> Value
MonadReader (Context Value, TemplateCache) SubM Source # |
Instance details Defined in Text.Mustache.Internal.Types Methods ask :: SubM (Context Value, TemplateCache) Source # local :: ((Context Value, TemplateCache) -> (Context Value, TemplateCache)) -> SubM a -> SubM a Source # reader :: ((Context Value, TemplateCache) -> a) -> SubM a Source #

substituteNode :: Node Text -> SubM () Source #

Main substitution function

substituteAST :: STree -> SubM () Source #

Substitute an entire `STree` rather than just a single `Node`

catchSubstitute :: SubM a -> SubM (a, Text) Source #

Catch the results of running the inner substitution.

# Util

toString :: Value -> SubM Text Source #

Converts values to Text as required by the mustache standard

# Orphan instances

ToMustache (Context Value -> STree -> STree) Source # |
Instance details Methods toMustache :: (Context Value -> STree -> STree) -> Value Source # listToMustache :: [Context Value -> STree -> STree] -> Value
ToMustache (Context Value -> STree -> Text) Source # |
Instance details Methods toMustache :: (Context Value -> STree -> Text) -> Value Source # listToMustache :: [Context Value -> STree -> Text] -> Value
ToMustache (Context Value -> STree -> Text) Source # |
Instance details Methods toMustache :: (Context Value -> STree -> Text) -> Value Source # listToMustache :: [Context Value -> STree -> Text] -> Value
ToMustache (Context Value -> STree -> String) Source # |
Instance details Methods toMustache :: (Context Value -> STree -> String) -> Value Source # listToMustache :: [Context Value -> STree -> String] -> Value
ToMustache (STree -> SubM Text) Source # |
Instance details Methods toMustache :: (STree -> SubM Text) -> Value Source # listToMustache :: [STree -> SubM Text] -> Value