예제 #1
0
            public override void MutateState(EmailRenderer renderer)
            {
                var iteratorParamGroups = renderer._emailParams.Where(x => IsIteratorParam(x.Key))
                                          .Select(x => new { param = x.Key.Prefix("::"), group = x.Key.Suffix("::"), value = x.Value })
                                          .GroupBy(x => x.group);

                //this gives us groups of parameters by iteration..
                //param = e.g. URL or Person.Name

                renderer._outputSb = renderer._finalSb;

                foreach (var iteratorParamGroup in iteratorParamGroups)
                {
                    var iteratorRenderer = new EmailRenderer(renderer._config);

                    var emailParamsForIterator = renderer._emailParams.Concat(iteratorParamGroup
                                                                              .Select(iteratorParam => new KeyValuePair <string, string>("{0}:Item{1}".FormatWith(_blockName, GetPropertyForIterator(iteratorParam.param)), iteratorParam.value)))
                                                 .ToDictionary(x => x.Key, x => x.Value);

                    var renderedIteration = iteratorRenderer.RenderFromTemplate(_blockTemplate, emailParamsForIterator);

                    renderer._outputSb.Append(renderedIteration);
                }

                renderer._iteratorBlock = null;
            }
예제 #2
0
            public override void MutateState(EmailRenderer renderer)
            {
                if (renderer._iteratorBlock == null)
                {
                    string value;

                    if (!renderer._emailParams.TryGetValue(_param, out value))
                    {
                        return;
                    }
                    //we were throwing here but it makes it to annoying in terms of placeholders
                    //that are not always required

                    renderer._outputSb.Append(value);
                }
            }
예제 #3
0
            public override void MutateState(EmailRenderer renderer)
            {
                string conditionalValue;

                if (!renderer._emailParams.TryGetValue(_param, out conditionalValue))
                {
                    throw new ErrorditeEmailParameterNotFoundException(_param);
                }

                PreviousSb = renderer._outputSb;

                renderer._conditionals.Push(this);

                if (conditionalValue.IsNullOrEmpty() || conditionalValue.ToLower() == "false")
                {
                    renderer._outputSb = new StringBuilder();
                    ElseSb             = PreviousSb;
                }
                else
                {
                    ElseSb = new StringBuilder();
                }
            }
예제 #4
0
 public override void MutateState(EmailRenderer renderer)
 {
     renderer._masterTemplateName = _masterTemplateName;
 }
예제 #5
0
 public override void MutateState(EmailRenderer renderer)
 {
     renderer._iteratorBlock = _blockName;
     renderer._harvestingSb.Clear();
     renderer._outputSb = renderer._harvestingSb;
 }
예제 #6
0
 public override void MutateState(EmailRenderer renderer)
 {
     renderer.ChangeToPlaceholder(_placeholderName);
 }
예제 #7
0
 public abstract void MutateState(EmailRenderer renderer);
예제 #8
0
 public override void MutateState(EmailRenderer renderer)
 {
     renderer._outputSb = renderer._conditionals.Pop().PreviousSb;
 }
예제 #9
0
            public override void MutateState(EmailRenderer renderer)
            {
                var conditional = renderer._conditionals.Peek();

                renderer._outputSb = conditional.ElseSb;
            }
예제 #10
0
 public override void MutateState(EmailRenderer renderer)
 {
     renderer._outputSb.Append("$({0})".FormatWith(_param));
 }
예제 #11
0
        public string RenderFromTemplate(string template, IDictionary <string, string> emailParams)
        {
            _state       = EmailRendererState.Normal;
            _emailParams = emailParams;
            _outputSb    = _finalSb;

            foreach (var c in template)
            {
                switch (_state)
                {
                case EmailRendererState.Normal:
                    if (c == '$')
                    {
                        _outputSb.Append(_bufferSb.ToString());
                        _bufferSb.Clear();
                        _bufferSb.Append(c);
                        _state = EmailRendererState.BeginParam;
                    }
                    else
                    {
                        _bufferSb.Append(c);
                    }
                    break;

                case EmailRendererState.BeginParam:
                    if (c == '(')
                    {
                        _bufferSb.Clear();
                        _state = EmailRendererState.InParam;
                    }
                    else
                    {
                        _state = EmailRendererState.Normal;
                    }
                    break;

                case EmailRendererState.InParam:
                    if (c == ')')
                    {
                        var directive = GetDirective();
                        directive.MutateState(this);
                        _bufferSb.Clear();
                        _state = EmailRendererState.Normal;
                    }
                    else
                    {
                        _bufferSb.Append(c);
                    }
                    break;
                }
            }

            _outputSb.Append(_bufferSb.ToString());

            if (_masterTemplateName != null)
            {
                var masterRenderer  = new EmailRenderer(_config, _templateLocator);
                var emailParamsCopy = emailParams.ToDictionary(x => x.Key, x => x.Value);

                foreach (var placeholder in _placeholderSbs)
                {
                    emailParamsCopy[placeholder.Key] = placeholder.Value.ToString();
                }

                if (!_placeholderSbs.Any())
                {
                    emailParamsCopy["Placeholder"] = _finalSb.ToString();
                }

                var masterTemplate = _templateLocator.GetTemplate(_masterTemplateName);
                return(masterRenderer.RenderFromTemplate(masterTemplate, emailParamsCopy));
            }

            return(_outputSb.ToString());
        }