Beispiel #1
0
 public void Add(Scope subScope)
 {
     SubScopes.Add(subScope);
     subScope.ParentScope = this;
 }
        protected internal string Render(Scope startScope, TemplateContext context)
        {
            _renderedTemplate.Clear();
            Context = context ?? TemplateContext.Empty;

            RenderScope(startScope);
            return _renderedTemplate.ToString();
        }
        protected internal void RenderScope(Scope scope)
        {
            _currentScope = scope;

            if (_currentScope.Start != null)
                _currentScope.ExprRenderer.Render(this);
            else
                RenderSubScopes();
        }
        private bool CreateScopes()
        {
            int level = 0;
            List<int> passedBlockExprs = new List<int>();

            _scope0 = new Scope(TemplateContent) { Level = level };

            Scope currScope = _scope0;

            for (int i = 0; i < ExprRenderers.Count; i++)
            {
                ExprRenderer currRenderer = ExprRenderers[i];
                if (currRenderer.RenderType == ExprRenderType.Simple)
                {
                    currRenderer.Level = level;
                    Scope scope = new Scope(TemplateContent)
                    {
                        ExprRenderer = currRenderer,
                        Level = level,
                        Start = currRenderer.ExprMatchResult,
                        End = currRenderer.ExprMatchResult
                    };
                    currScope.Add(scope);
                }
                else
                {
                    if (currRenderer.RenderType == ExprRenderType.BlockStart)
                        level += 1;

                    currRenderer.Level = level;

                    if (currRenderer.RenderType == ExprRenderType.BlockStart)
                    {
                        Scope scope = new Scope(TemplateContent)
                        {
                            Level = level,
                            ExprRenderer = currRenderer,
                            Start = currRenderer.ExprMatchResult
                        };
                        currScope.Add(scope);
                        currScope = scope;
                    }
                    else
                    {
                        currScope.End = currRenderer.ExprMatchResult;
                        currScope = currScope.ParentScope;

                        if (currRenderer.RenderType == ExprRenderType.BlockMiddle)
                        {
                            Scope scope = new Scope(TemplateContent)
                            {
                                Level = level,
                                ExprRenderer = currRenderer,
                                Start = currRenderer.ExprMatchResult
                            };

                            currScope.Add(scope);

                            if (currRenderer.JointRendererName != null)
                            {
                                for (int j = i - 1; j >= 0; j--)
                                {
                                    ExprRenderer render_j = ExprRenderers[j];
                                    if (render_j.Level == render_j.Level
                                        && render_j.Name == currRenderer.JointRendererName)
                                    {
                                        currRenderer.JointRenderer = render_j;
                                        break;
                                    }
                                }
                            }

                            currScope = scope;
                            continue;
                        }
                    }

                    if (currRenderer.RenderType == ExprRenderType.BlockEnd || currRenderer.RenderType == ExprRenderType.BlockMiddle)
                    {
                        int blockStarExprIndex = GetBlockStartExprIndex(currRenderer, i);
                        if (ContainsErrors)
                            return false;

                        passedBlockExprs.Add(blockStarExprIndex);
                    }

                    if (currRenderer.RenderType == ExprRenderType.BlockEnd)
                        level -= 1;
                }
            }

            for (int i = 0; i < ExprRenderers.Count; i++)
            {
                if (!passedBlockExprs.Contains(i))
                {
                    ExprRenderer er_i = ExprRenderers[i];
                    if (er_i.RenderType == ExprRenderType.BlockStart)
                    {
                        if (er_i.BlockDef == null)
                            throw new TemplateException(string.Format("[FATAL] Missing block definition, line {0}", er_i.ExprMatchResult.Line), this);

                        Errors.Add(new TemplateError(er_i.ExprMatchResult.Line, string.Format("Missing '{0}' block end instruction, line {1}",
                                                                                                er_i.BlockDef.Name,
                                                                                                er_i.ExprMatchResult.Line)));
                    }
                }
            }

            _scope0.ExtractStaticTextRecursive();

            return ContainsErrors;
        }
 public void RenderSubScopes()
 {
     Scope currentScopeSave = _currentScope;
     _currentScope.RenderSubScopes(this);
     _currentScope = currentScopeSave;
 }
        internal object this[Scope scope, TemplateVar variable, List<TemplateFilter> filters]
        {
            get {
                object val = null;
                if (variable != null && variable.StrValue != null)
                {
                    if (variable.IsLiteralString)
                        val = variable.StrValue;
                    else
                    {
                        string varStr = variable.StrValue;
                        if (!string.IsNullOrWhiteSpace (varStr))
                        {
                            if (!GetOverride (scope, varStr, out val))
                            {
                                string[] varSplit = varStr.Split ('.');
                                if (GetOverride (scope, varSplit [0], out val))
                                {
                                    if (val != null && varSplit.Length > 1)
                                        val = ReadSubProperty (val, varSplit, 1);
                                } else
                                {
                                    int i;
                                    if (int.TryParse (variable.StrValue, out i))
                                        val = i;
                                    else
                                    {
                                        double d;
                                        if (double.TryParse (variable.StrValue, out d))
                                            val = d;
                                    }
                                }
                            }
                        }
                    }
                }

                if (filters != null && filters.Count > 0)
                {
                    int filtersCount = filters.Count;
                    KeyValuePair<string, object>[] resolvedFilters = new KeyValuePair<string, object>[filtersCount];
                    for (int i = 0; i < filtersCount; i++)
                    {
                        TemplateFilter currFilter = filters [i];
                        resolvedFilters [i] = new KeyValuePair<string, object> (currFilter.Name, this [scope, currFilter.Argument, null]);
                    }

                    return FilterManager.Filter (val, resolvedFilters);
                } else
                    return val;
            }
        }
        internal void PushOverride(Scope scope, string objName, object value)
        {
            if (_alwaysEmpty)
                return;

            if (!_overrides.ContainsKey(scope))
                _overrides.Add(scope, new Dictionary<string, object>());
            _overrides[scope][objName] = value;
        }
        internal void PopOverride(Scope scope, string objName)
        {
            if (_alwaysEmpty)
                return;

            if (_overrides.ContainsKey(scope))
                _overrides[scope].Remove(objName);
        }
        internal bool GetOverride(Scope scope, string objname, out object result)
        {
            if (_alwaysEmpty) {
                result = null;
                return false;
            }

            if (_overrides.ContainsKey (scope) && _overrides [scope].ContainsKey (objname))
                result = _overrides [scope] [objname];
            else if (scope.ParentScope != null)
                return GetOverride (scope.ParentScope, objname, out result);
            else if (_objects.ContainsKey (objname))
                result = _objects [objname];
            else {
                result = null;
                return false;
            }

            return true;
        }
 internal bool Contains(Scope scope, string objname)
 {
     return !_alwaysEmpty && _overrides.ContainsKey(scope) && _overrides[scope].ContainsKey(objname);
 }