Пример #1
0
        public void Render(IncludeTag includeTag, ITemplateContext templateContext)
        {
            var virtualFilenameVar = LiquidExpressionEvaluator.Eval(includeTag.VirtualFileExpression, templateContext);
            if (virtualFilenameVar.IsError)
            {
                _renderingVisitor.Errors.Add(virtualFilenameVar.ErrorResult);
                return;  
            }

            String virtualFileName = ValueCaster.RenderAsString(virtualFilenameVar.SuccessResult.Value);

            if (templateContext.FileSystem == null)
            {
                _renderingVisitor.Errors.Add(new LiquidError{Message = " ERROR: FileSystem is not defined"});
                return;
            }

            String snippet = templateContext.FileSystem.Include(templateContext, virtualFileName);
            templateContext.SymbolTableStack.DefineLocalRegistry(LOCALREGISTRY_FILE_KEY, virtualFileName);
            LiquidAST snippetAst;
            try
            {
                //snippetAst = GenerateSnippetAst(snippet);
                snippetAst = templateContext.ASTGenerator(snippet);
            }
            catch (LiquidParserException ex)
            {
                foreach (var error in ex.LiquidErrors)
                {
                    if (String.IsNullOrEmpty(error.TokenSource))
                    {
                        error.TokenSource = virtualFileName;
                    }
                }
                throw;
            }

            if (includeTag.ForExpression != null)
            {
                var forExpressionOption = LiquidExpressionEvaluator.Eval(includeTag.ForExpression, templateContext);
                if (forExpressionOption.IsError)
                {
                    _renderingVisitor.Errors.Add(forExpressionOption.ErrorResult);
                    return;
                }
                if (forExpressionOption.SuccessResult.Value is DictionaryValue) // it seems to render as a single element if it's a dictionary.
                {
                    var localBlockScope = new SymbolTable();
                    DefineLocalVariables(templateContext, localBlockScope, includeTag.Definitions);

                    var exprValue = LiquidExpressionEvaluator.Eval(includeTag.ForExpression, templateContext);
                    localBlockScope.DefineLocalVariable(virtualFileName, exprValue.SuccessResult.Value);

                    RenderWithLocalScope(templateContext, localBlockScope, snippetAst.RootNode);
                }
                else
                {
                    //ArrayValue array = ValueCaster.Cast<IExpressionConstant, ArrayValue>(forExpressionOption.SuccessResult.Value);
                    var arrayResult = ValueCaster.Cast<IExpressionConstant, ArrayValue>(forExpressionOption.SuccessResult.Value);
                    if (arrayResult.IsError)
                    {
                        _renderingVisitor.Errors.Add(arrayResult.ErrorResult);
                        return;
                    }

//                    if (array.HasError)
//                    {
//                        _renderingVisitor.Errors.Add(new LiquidError {Message = array.ErrorMessage});
//                        return;
//                    }
                    foreach (Option<IExpressionConstant> val in arrayResult.SuccessValue<ArrayValue>())
                    {
                        var localBlockScope = new SymbolTable();
                        DefineLocalVariables(templateContext, localBlockScope, includeTag.Definitions);
                        if (val.HasValue)
                        {
                            localBlockScope.DefineLocalVariable(virtualFileName, val.Value);
                        }
                        else
                        {
                            localBlockScope.DefineLocalVariable(virtualFileName, null);
                        }
                        RenderWithLocalScope(templateContext, localBlockScope, snippetAst.RootNode);
                    }
                }
            }
            else
            {
                var localBlockScope = new SymbolTable();
                DefineLocalVariables(templateContext, localBlockScope, includeTag.Definitions);
                if (includeTag.WithExpression != null)
                {
                    var withExpression = LiquidExpressionEvaluator.Eval(includeTag.WithExpression, templateContext);
                    localBlockScope.DefineLocalVariable(virtualFileName, withExpression.SuccessResult.Value);
                }
                RenderWithLocalScope(templateContext, localBlockScope, snippetAst.RootNode);
            }
           
                       
        }
Пример #2
0
 public void Visit(IncludeTag includeTag)
 {
     _result += includeTag.ToString();
 }
Пример #3
0
        public void Visit(IncludeTag includeTag)
        {

            var includeRenderer = new IncludeRenderer(this);
            includeRenderer.Render(includeTag, _templateContext);
 
        }