TextWriter _writer; // all output is sent here

        #endregion Fields

        #region Constructors

        /// <summary>
        /// create template manager using a template
        /// </summary>
        public TemplateHelper(Template template)
        {
            this._mainTemplate = template;
            this._currentTemplate = template;

            Init();
        }
        public Template(string name, List<Element> elements, Template parent)
        {
            this.name = name;
            this.elements = elements;
            this.parent = parent;

            InitTemplates();
        }
        /// <summary>
        /// go thru all tags and see if they are template tags and add
        /// them to this.templates collection
        /// </summary>
        private void InitTemplates()
        {
            this.templates = new Dictionary<string, Template>(StringComparer.InvariantCultureIgnoreCase);

            foreach (Element elem in elements)
            {
                if (elem is Tag)
                {
                    Tag tag = (Tag)elem;
                    if (string.Compare(tag.Name, "template", true) == 0)
                    {
                        Expression ename = tag.AttributeValue("name");
                        string tname;
                        if (ename is StringLiteral)
                            tname = ((StringLiteral)ename).Content;
                        else
                            tname = "?";

                        Template template = new Template(tname, tag.InnerElements, this);
                        templates[tname] = template;
                    }
                }
            }
        }
        protected void ProcessTemplate(string name, Tag tag)
        {
            Template useTemplate = _currentTemplate.FindTemplate(name);
            if (useTemplate == null)
            {
                string msg = string.Format("Template '{0}' not found", name);
                WriteError(msg, tag.Line, tag.Col);
                return;
            }

            // process inner elements and save content
            TextWriter saveWriter = _writer;
            _writer = new StringWriter();
            string content = string.Empty;

            try
            {
                ProcessElements(tag.InnerElements);

                content = _writer.ToString();
            }
            catch
            {
                return; // on error, do not do tag inclusion
            }
            finally
            {
                _writer = saveWriter;
            }

            Template saveTemplate = _currentTemplate;
            _variables = new VariableScope(_variables);
            _variables["innerText"] = content;

            try
            {
                foreach (TagAttribute attrib in tag.Attributes)
                {
                    object val = EvalExpression(attrib.Expression);
                    _variables[attrib.Name] = val;
                }

                _currentTemplate = useTemplate;
                ProcessElements(_currentTemplate.Elements);
            }
            finally
            {
                _variables = _variables.Parent;
                _currentTemplate = saveTemplate;
            }
        }
        /// <summary>
        /// processes current template and sends output to writer
        /// </summary>
        /// <param name="writer"></param>
        public void Process(TextWriter writer)
        {
            this._writer = writer;
            this._currentTemplate = _mainTemplate;

            ProcessElements(_mainTemplate.Elements);
        }
 /// <summary>
 /// adds template that can be used within execution 
 /// </summary>
 /// <param name="template"></param>
 public void AddTemplate(Template template)
 {
     _mainTemplate.Templates.Add(template.Name, template);
 }