diff --git a/Text/HPaco/Writers/Run.hs b/Text/HPaco/Writers/Run.hs
--- a/Text/HPaco/Writers/Run.hs
+++ b/Text/HPaco/Writers/Run.hs
@@ -1,6 +1,6 @@
 {-#LANGUAGE ScopedTypeVariables #-}
 module Text.HPaco.Writers.Run
-        ( run
+        ( run, runWith
         , RunState (..)
         , RunOptions (..)
         , defaultOptions
@@ -12,6 +12,7 @@
 
 import Prelude hiding (toInteger)
 import Data.Variant
+import Data.Variant.ToFrom
 import qualified Data.Variant as V
 import Data.Maybe
 import Data.Monoid
@@ -25,13 +26,16 @@
 import Text.HPaco.AST.AST
 import Text.HPaco.AST.Statement
 import Text.HPaco.AST.Expression
+import System.IO
 
 data RunOptions = RunOptions
                     { roTemplateName :: String
+                    , roOutput :: Handle
                     }
 
 defaultOptions = RunOptions
                     { roTemplateName = "unnamed"
+                    , roOutput = stdout
                     }
 
 data RunState = RunState
@@ -43,8 +47,11 @@
 type Run a = StateT RunState IO a
 
 run :: RunOptions -> AST -> IO ()
-run opts ast = do
-    let st = RunState { rsScope = AList [], rsOptions = opts , rsAST = ast }
+run = runWith $ AList []
+
+runWith :: ToVariant a => a -> RunOptions -> AST -> IO ()
+runWith scope opts ast = do
+    let st = RunState { rsScope = toVariant scope, rsOptions = opts , rsAST = ast }
     execStateT (runAST ast) st
     return ()
 
@@ -61,7 +68,8 @@
 runStatement :: Statement -> Run ()
 runStatement (PrintStatement e) = do
     d <- runExpression e
-    liftIO . putStr . flatten $ d
+    h <- gets (roOutput . rsOptions)
+    liftIO . hPutStr h . flatten $ d
 runStatement (StatementSequence ss) = mapM_ runStatement ss
 runStatement (IfStatement cond true false) = do
     b <- liftM toBool $ runExpression cond
diff --git a/hpaco-lib.cabal b/hpaco-lib.cabal
--- a/hpaco-lib.cabal
+++ b/hpaco-lib.cabal
@@ -1,5 +1,5 @@
 name:                hpaco-lib
-version:             0.22.4.0
+version:             0.24.0.0
 synopsis:            Modular template compiler library
 description:         Template compiler library, compiles template code into
                      PHP or Javascript, or interprets it directly.
