예제 #1
0
        public string Evaluate(TagModel model)
        {
            var id = GetAutoValueAsString(nameof(Name), model);

            if (id == null)
            {
                throw ExpressionParseException.UnexpectedNullValue(nameof(Name)).Decorate(Name.Context);
            }
            id = LanguageHelper.CamelCaseAttribute(id);
            var raw = model.TryGet(id);

            if (raw == null)
            {
                throw MacroException.NotFound(id).Decorate(Name.Context);
            }
            var macro    = raw as DefineMacroTag.MarcoDefinition;
            var function = raw as DefineFunctionTag.FunctionDefinition;

            if (macro == null && function == null)
            {
                throw MacroException.NoMacroOrFunction(id).Decorate(Name.Context);
            }
            if (macro != null)
            {
                return(ExecuteMacro(model, id, macro));
            }
            return(ExecuteFunction(model, function));
        }
        private void AddAttributes(ITagAttributeSetter tagReflection, ITag tag)
        {
            while (!_helper.IsAhead(CLOSING_TOKENS))
            {
//                _helper.Read(TokenType.Seperator);
                _helper.Next();
                ReadWhiteSpace(_helper);
                if (_helper.IsAhead(CLOSING_TOKENS))
                {
                    return;
                }
                var keyToken = _helper.Read(TokenType.Regular);
                var key      = tagReflection.SupportNaturalLanguage?LanguageHelper.CamelCaseAttribute(keyToken.Contents): keyToken.Contents;
                _helper.Read(TagLibConstants.FIELD_ASSIGNMENT);
                var value    = _helper.Read(TokenType.Literal).Contents;
                var existing = tagReflection[key];
                if (!existing?.AllowOverWrite ?? false)
                {
                    throw TagException.PropertyAlReadySet(key).Decorate(keyToken.Context);
                }
                if (string.IsNullOrEmpty(value))
                {
                    tagReflection[key] = new ConstantAttribute("", tag)
                    {
                        AttributeName = key, Context = keyToken.Context
                    };
                    continue;
                }
                if (!value.Contains("${"))
                {
                    tagReflection[key] = new ConstantAttribute(value, tag)
                    {
                        AttributeName = key, Context = keyToken.Context, ResourceLocator = _locator
                    };
                    continue;
                }
                var offSet = _helper.Current.Context;
                var attr   = new TemplateAttribute(new InternalFormatter(new TagLibParserFactoryAdapter(this), _expressionLib, value, false, _locator, offSet).Parse())
                {
                    AttributeName = key
                };
                tagReflection[key] = attr;
            }
        }
예제 #3
0
        public static string Evaluate(ITagWithVariable tag, TagModel model, bool naturalLanguage = false)
        {
            var result = tag.InternalEvaluate(model);
            var var    = tag.GetAutoValueAsString("Var", model);

            if (naturalLanguage)
            {
                var = LanguageHelper.CamelCaseAttribute(var);
            }
            var scope = tag.GetAutoValueAs <VariableScope>("Scope", model);

            if (scope != VariableScope.Page || !model.TryUpdateTag(var, result))
            {
                model[scope + "." + var] = result;
            }
            var postTag = tag as ITagWithVariableAndPostEvaluate;

            postTag?.PostEvaluate(model, result);
            return(string.Empty);
        }