private string RenderVariable(
            string param,
            Dictionary <string, object> variableDictionary)
        {
            string       objectParam;
            int?         objectArrayIndex;
            IHasTemplate obj = GetVarObject(param, variableDictionary, out objectParam, out objectArrayIndex) as IHasTemplate;

            if (obj == null)
            {
                obj = GetVariable(objectParam, variableDictionary, objectArrayIndex) as IHasTemplate;
            }

            if (string.IsNullOrEmpty(obj.TemplateName()))
            {
                ThrowTemplateException(
                    string.Format("Render called but the object does not have a template: {0}. {1}.",
                                  param,
                                  obj.ToString()));
            }

            IHasProps objProps = obj as IHasProps;


            variableDictionary["this"] = objProps;
            string rv = FormatTemplate(obj.TemplateName(), variableDictionary);

            variableDictionary.Remove("this");
            return(rv);
        }
        // From:
        // {props:filter.input[i].Name}
        // Return:
        // objectParam = 'Name'
        // Rv = input[i]
        private IHasProps GetPropsObject(
            string param,
            Dictionary <string, object> variableDictionary,
            out string objectParam,
            out int?objectArrayIndex)
        {
            // Tokenize by dot
            string[] dotTokens = param.Split('.');

            IHasProps currentObject = this;

            for (int i = 0; ; ++i)
            {
                // Get array index if specified
                objectParam      = dotTokens[i];
                objectArrayIndex = ParseArrayNotation(variableDictionary, ref objectParam);

                bool lastItem = (i == dotTokens.Length - 1);
                if (lastItem)
                {
                    break;
                }

                // Get the next object
                currentObject = currentObject.FormatProps(objectParam, objectArrayIndex) as IHasProps;
            }

            return(currentObject);
        }
        private object FormatProps(
            string param,
            Dictionary <string, object> variableDictionary)
        {
            string    objectParam;
            int?      objectArrayIndex;
            IHasProps obj = GetPropsObject(param, variableDictionary, out objectParam, out objectArrayIndex);

            return(obj.FormatProps(objectParam, objectArrayIndex));
        }
        // From:
        // {var:filter.input[i].Name}
        // Return:
        // objectParam = 'Name'
        // Rv = input[i]
        private IHasProps GetVarObject(
            string param,
            Dictionary <string, object> variableDictionary,
            out string objectParam,
            out int?objectArrayIndex)
        {
            // Tokenize by dot
            string[] dotTokens = param.Split('.');

            IHasProps currentObject = null;

            for (int i = 0; ; ++i)
            {
                // Get array index if specified
                objectParam      = dotTokens[i];
                objectArrayIndex = ParseArrayNotation(variableDictionary, ref objectParam);

                bool isLastIteration = (i == dotTokens.Length - 1);

                // First token?
                if (i == 0)
                {
                    if (isLastIteration)
                    {
                        break;
                    }

                    // Get from the dictionary
                    object obj = GetVariable(objectParam, variableDictionary, objectArrayIndex);
                    currentObject = obj as IHasProps;
                }
                else
                {
                    if (isLastIteration)
                    {
                        break;
                    }

                    // get the next object
                    currentObject = currentObject.FormatProps(objectParam, objectArrayIndex) as IHasProps;
                }
            }

            return(currentObject);
        }
        //{for:i = 0 to countVar:filter.input}
        private int FormatCountVar(
            string param,
            Dictionary <string, object> variableDictionary)
        {
            string    objectParam;
            int?      objectArrayIndex;
            IHasProps obj = GetVarObject(param, variableDictionary, out objectParam, out objectArrayIndex);
            int       rv;

            if (obj == null)
            {
                // This doesn't make sense. For exameple:
                //{for:i = 0 to countVar:i} <-- this is the error. why not just do below
                //{for:i = 0 to var:i}
                ThrowTemplateException(string.Format("Invalid count variable: {0}", param));
            }
            rv = obj.Count(objectParam);
            return(rv);
        }
        private object FormatVariable(
            string param,
            Dictionary <string, object> paramDictionary)
        {
            string    objectParam;
            int?      objectArrayIndex;
            IHasProps obj = GetVarObject(param, paramDictionary, out objectParam, out objectArrayIndex);
            object    rv;

            if (obj != null)
            {
                rv = obj.FormatProps(objectParam, objectArrayIndex, true);
            }
            else
            {
                rv = GetVariable(objectParam, paramDictionary, objectArrayIndex);
            }

            return(rv);
        }