# Templates Pencil comes with a simple templating engine. Templates allow us to build web pages dynamically using Haskell code. Every blog post, for example, can share a common HTML template. Pencil templates are regular HTML and Markdown (and other formats that Pencil supports) files that contain a **preamble** or **directives**. ## Preamble Preambles are environment variable declarations inside your source files. A file may only have one preamble, and they must be declared at the top of the file. A preamble is YAML wrapped in an HTML comment. An example Markdown file with a preamble: ``` I learned Java first. So when I found out about Python, it was *love at first sight*. ``` In the above example, Pencil will intelligently parse the `date` value as a [`VDateTime`](https://hackage.haskell.org/package/pencil/docs/Pencil-Internal-Env.html#v:VDateTime). See [`toDateTime`](https://hackage.haskell.org/package/pencil/docs/Pencil-Internal-Env.html#v:toDateTime), which describes the date formats that Pencil will try to coerce into a `VDateTime`. ## Directives Directives allow you to render variables. They are surrounded by `$${...}`. ### Variables The simplest directive is the variable directive. ``` Hello $${name}! ``` The above template will render the value of the variable `name`, which is expected to be in the environment at `render`. If the variable is not found, the final render will literally include `${name}`. ### If block The `if` directive allows us to render content based off the existence of a variable in the current environment. ``` $${if(name)} Hello $${name}! $${end} ``` In this case, we now make sure that `name` is available before rendering. ### For loop The `for` directive allows for looping over arrays. This is useful for things like rendering a list of blog post titles, linking each line to the actual blog post. ```