         @emit 'Reload'
 
       callback err, stackTemplate
+
+
+  handlePreview: ->
+
+    template      = @editorView.getValue()
+
+    group         = kd.singletons.groupsController.getCurrentGroup()
+    account       = whoami()
+    availableData = { group, account }
+
+    requiredData  = requirementsParser template
+    errors        = []
+
+    fetchUserData = (callback) ->
+      account.fetchFromUser requiredData.user, (err, data) ->
+        kd.warn err  if err
+        callback data ? {}
+
+    generatePreview = =>
+
+      for type, data of requiredData
+        for field in data
+          if content = jspath.getAt availableData[type], field
+            template = template.replace \
+              (new RegExp "{{#{type} #{field}}}", 'g'), content
+          else
+            errors.push "Variable `#{field}` not found in `#{type}` data."
+
+      if errors.length > 0
+        console.warn "Errors for preview requirements: ", errors
+
+        errors = " - #{error}\n" for error in errors
+        errors = "> Following errors found while generating
+                  preview for this template: \n#{errors}"
+      else
+        errors = ''
+
+      new kd.ModalView
+        title          : 'Template Preview'
+        subtitle       : 'Generated from your account data'
+        cssClass       : 'has-markdown content-modal'
+        height         : 500
+        overlay        : yes
+        overlayOptions : cssClass : 'second-overlay'
+        content        : applyMarkdown """
+          #{errors}
+          ```coffee
+          #{template}
+          ```
+        """
+
+      @previewButton.hideLoader()
+
+    if requiredData.user?
+      fetchUserData (data) =>
+        availableData.user = data
+        generatePreview()
+    else
+      generatePreview()
 
 
   handleSetDefaultTemplate: ->

      {{> @outputView}}
      {{> @cancelButton}}
      {{> @setAsDefaultButton}}
      {{> @saveButton}}
    """
kd                   = require 'kd'
jspath               = require 'jspath'

JView                = require 'app/jview'
whoami               = require 'app/util/whoami'
curryIn              = require 'app/util/curryIn'
showError            = require 'app/util/showError'
applyMarkdown         = require 'app/util/applyMarkdown'

{yamlToJson}         = require './yamlutils'
providersParser      = require './providersparser'
