Exemple #1
0
 public override Expression GetAccumulatedBlock()
 {
     return(HandlebarsExpression.PartialExpression(
                _startingNode.PartialName,
                _startingNode.Argument,
                _body.Count > 1 ? Expression.Block(_body) : _body.First()));
 }
        public IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                if (item is PartialToken)
                {
                    var arguments = AccumulateArguments(enumerator);
                    if (arguments.Count == 0)
                    {
                        throw new HandlebarsCompilerException("A partial must have a name");
                    }

                    var partialName = arguments[0];

                    if (partialName is PathExpression pathExpression)
                    {
                        partialName = Expression.Constant(pathExpression.Path);
                    }

                    switch (arguments.Count)
                    {
                    case 1:
                        yield return(HandlebarsExpression.PartialExpression(partialName));

                        break;

                    case 2:
                        yield return(HandlebarsExpression.PartialExpression(partialName, arguments [1]));

                        break;

                    default:
                        throw new HandlebarsCompilerException("A partial can only accept 0 or 1 arguments");
                    }
                    yield return(enumerator.Current);
                }
                else
                {
                    yield return(item);
                }
            }
        }
Exemple #3
0
 private PartialExpression ConvertToPartialExpression(Expression expression)
 {
     if (expression is PathExpression pathExpression)
     {
         _blockName = pathExpression.Path.Replace("#>", "");
         return(HandlebarsExpression.PartialExpression(Expression.Constant(_blockName)));
     }
     if (expression is HelperExpression helperExpression)
     {
         _blockName = helperExpression.HelperName.Replace("#>", "");
         if (!helperExpression.Arguments.Any())
         {
             return(HandlebarsExpression.PartialExpression(Expression.Constant(_blockName)));
         }
         if (helperExpression.Arguments.Count() == 1)
         {
             return(HandlebarsExpression.PartialExpression(
                        Expression.Constant(_blockName),
                        helperExpression.Arguments.First()));
         }
         throw new InvalidOperationException("Cannot convert a multi-argument helper expression to a partial expression");
     }
     throw new InvalidOperationException(string.Format("Cannot convert '{0}' to a partial expression", expression));
 }