Blame README.md

Packit 938058
# doctemplates
Packit 938058
Packit 938058
This is the templating system used by pandoc.  It was formerly
Packit 938058
be a module in pandoc. It has been split off to make it easier
Packit 938058
to use independently.
Packit 938058
Packit 938058
Example:
Packit 938058
Packit 938058
``` haskell
Packit 938058
{-# LANGUAGE OverloadedStrings #-}
Packit 938058
import Data.Text
Packit 938058
import Data.Aeson
Packit 938058
import Text.DocTemplates
Packit 938058
Packit 938058
data Employee = Employee { firstName :: String
Packit 938058
                         , lastName  :: String
Packit 938058
                         , salary    :: Maybe Int }
Packit 938058
instance ToJSON Employee where
Packit 938058
  toJSON e = object [ "name" .= object [ "first" .= firstName e
Packit 938058
                                       , "last"  .= lastName e ]
Packit 938058
                    , "salary" .= salary e ]
Packit 938058
Packit 938058
template :: Text
Packit 938058
template = "$for(employee)$Hi, $employee.name.first$. $if(employee.salary)$You make $employee.salary$.$else$No salary data.$endif$$sep$\n$endfor$"
Packit 938058
Packit 938058
main = case compileTemplate template of
Packit 938058
         Left e    -> error e
Packit 938058
         Right t   -> putStrLn $ renderTemplate t $ object
Packit 938058
                        ["employee" .=
Packit 938058
                          [ Employee "John" "Doe" Nothing
Packit 938058
                          , Employee "Omar" "Smith" (Just 30000)
Packit 938058
                          , Employee "Sara" "Chen" (Just 60000) ]
Packit 938058
                        ]
Packit 938058
```
Packit 938058
Packit 938058
A slot for an interpolated variable is a variable name surrounded
Packit 938058
by dollar signs.  To include a literal `$` in your template, use
Packit 938058
`$$`.  Variable names must begin with a letter and can contain letters,
Packit 938058
numbers, `_`, `-`, and `.`.
Packit 938058
Packit 938058
The values of variables are determined by a JSON object that is
Packit 938058
passed as a parameter to `renderTemplate`.  So, for example,
Packit 938058
`title` will return the value of the `title` field, and
Packit 938058
`employee.salary` will return the value of the `salary` field
Packit 938058
of the object that is the value of the `employee` field.
Packit 938058
Packit 938058
The value of a variable will be indented to the same level as the
Packit 938058
variable.
Packit 938058
Packit 938058
A conditional begins with `$if(variable_name)$` and ends with `$endif$`.
Packit 938058
It may optionally contain an `$else$` section.  The if section is
Packit 938058
used if `variable_name` has a non-null value, otherwise the else section
Packit 938058
is used.
Packit 938058
Packit 938058
Conditional keywords should not be indented, or unexpected spacing
Packit 938058
problems may occur.
Packit 938058
Packit 938058
The `$for$` keyword can be used to iterate over an array.  If
Packit 938058
the value of the associated variable is not an array, a single
Packit 938058
iteration will be performed on its value.
Packit 938058
Packit 938058
You may optionally specify separators using `$sep$`, as in the
Packit 938058
example above.
Packit 938058
Packit 938058
Anything between the sequence `$--` and the end of the line
Packit 938058
will be treated as a comment.