示例#1
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     chain = new RenderChain(chain, this);
     context = new Context(context, null, this.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as Object));
     var helper = dust.Helpers[this.Name];
     return helper.Render(dust, chain, context, model);
 }
示例#2
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     XDustNode block = chain.GetBlock(this.Name);
     if (null != block)
     {
         return block.Render(dust, chain, context, model);
     }
     else
     {
         return base.Render(dust, chain, context, model);
     }
 }
示例#3
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     chain = new RenderChain(chain, this);
     Object originalModel = model;
     model = this.Context.Resolve(context, model);
     String result = String.Empty;
     if (null != model)
     {
         if (model is XDustNode)
         {
             result = ((XDustNode)model).Render(dust, chain, context, originalModel);
         }
         else if (model is ContextResolver)
         {
             Object temp = ((ContextResolver)model).Resolve(context, originalModel);
             if (null != temp)
             {
                 result = temp.ToString();
             }
         }
         else if (model is IScriptable)
         {
             Object temp = ((IScriptable)model).Value;
             if (null != temp)
             {
                 result = temp.ToString();
             }
         }
         else
         {
             result = model.ToString();
         }
         if (this.Filters.Count > 0)
         {
             foreach (String flag in this.Filters)
             {
                 result = dust.Filters[flag].Invoke(result);
             }
         }
         else
         {
             SecurityElement.Escape(result);
         }
     }
     return result;
 }
示例#4
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     if (null != this.Scope)
     {
         model = this.Scope.Resolve(context, model);
     }
     chain = new RenderChain(chain, this);
     String name;
     if (this.Include is XDustNodeList)
     {
         name = ((XDustNodeList)this.Include).Render(dust, chain, context, model);
     }
     else
     {
         name = this.Include.ToString();
     }
     XDustNode template = dust.Load(null, name);
     return template.Render(dust, chain, context, model);
 }
示例#5
0
 public XDustNodeList Parse(XDust dust, String str)
 {
     Regex comments = new Regex(@"\{!.+?!\}", RegexOptions.Singleline);
     str = comments.Replace(str, String.Empty).Trim();
     var nodes = new List<XDustNodeList>();
     nodes.Add(new XDustNodeList(null));
     int depth = 0;
     Regex exp = new Regex(@"(\{[\~\#\?\@\:\<\>\+\/\^]?([a-zA-Z0-9_\$\.]+|""[^""]+"")(\:[a-zA-Z0-9\$\.]+)?(\|[a-z]+)*?( \w+\=((""[^""]*?"")|([\w\.]+)))*?\/?\})", RegexOptions.Singleline);
     int lastEnd = 0;
     int? start = null;
     int? end = null;
     Match match = exp.Match(str);
     while (match.Success)
     {
         bool depthChange = false;
         start = match.Index;
         end = start + match.Length;
         if (lastEnd != start)
         {
             String head;
             int headDiff = start.Value - lastEnd;
             if (headDiff <= 0)
             {
                 head = str.Substring(lastEnd);
             }
             else
             {
                 head = str.Substring(lastEnd, headDiff);
             }
             if (!String.IsNullOrEmpty(head))
             {
                 nodes[depth].Add(new XDustTextNode(head));
             }
         }
         lastEnd = end.Value;
         XDustNode node = null;
         String tag = str.Substring(start.Value + 1, end.Value - start.Value - 2);
         Char op = tag[0];
         String[] tagParts = tag.Split(' ');
         if (Operators.Contains(op))
         {
             String[] tagNameParts = tagParts[0].Substring(1).Split(':');
             bool selfClosed = tagNameParts[tagNameParts.Length - 1].EndsWith("/");
             if (selfClosed)
             {
                 int index = tagNameParts.Length - 1;
                 String last = tagNameParts[index];
                 tagNameParts[index] = last.Substring(0, last.Length - 1);
             }
             String scope = tagNameParts.Length > 1 ? tagNameParts[1] : null;
             String tagName = tagNameParts[0];
             Dictionary<String, XDustNode> parameters = null;
             if (op == '~')
             {
                 node = new XDustEscapedCharacterNode(tagName);
                 selfClosed = true;
             }
             else if (op == '#')
             {
                 if (dust.Helpers.ContainsKey(tagName))
                 {
                     node = new XDustHelperNode(tagName, scope, parameters);
                 }
                 else
                 {
                     node = new XDustLogicNode(tagName, scope, parameters);
                 }
             }
             else if (op == '?')
             {
                 node = new XDustExistsNode(tagName, scope, parameters);
             }
             else if (op == '@')
             {
                 String name = tagParts[0].Substring(1);
                 if (name == "idx")
                 {
                     node = new XDustIndexNode(null);
                 }
                 else if (name == "sep")
                 {
                     node = new XDustSepNode(null);
                 }
                 else
                 {
                     throw new InvalidOperationException();
                 }
             }
             else if (op == '>')
             {
                 bool isExternal = tagName.StartsWith("\"");
                 Object tagArgs = tagName;
                 if (isExternal)
                 {
                     tagArgs = this.Parse(dust, tagName.Trim('"'));
                 }
                 node = new XDustPartialNode(tagArgs, scope);
             }
             else if (op == '+')
             {
                 node = new XDustBlockNode(tagName);
             }
             else if (op == '<')
             {
                 node = new XDustInlinePartialNode(tagName);
             }
             else if (op == '/' || op == ':')
             {
                 node = null;
             }
             else
             {
                 throw new ArgumentOutOfRangeException();
             }
             if (!selfClosed)
             {
                 if (SectionOperators.Contains(op))
                 {
                     depthChange = true;
                     foreach (String param in tagParts.Skip(1))
                     {
                         String[] paramParts = param.Split('=');
                         String name = paramParts[0];
                         String valueStr = String.Join("=", paramParts.Skip(1).ToArray());
                         XDustNode value;
                         if (!valueStr.StartsWith("\""))
                         {
                             value = new XDustVariableNode(valueStr, null);
                         }
                         else
                         {
                             value = this.Parse(dust, valueStr.Trim('"'));
                         }
                         ((IXDustSectionNode)node).Parameters[name] = value;
                     }
                     if (node is XDustNodeList)
                     {
                         if (node is XDustInlinePartialNode)
                         {
                             nodes[depth].SetBlock(((XDustInlinePartialNode)node).Name, node);
                         }
                         nodes[depth].Add(node);
                         depth += 1;
                         nodes.Insert(depth, (XDustNodeList)node);
                     }
                     else if (node is XDustLogicNode)
                     {
                         nodes[depth].Add(node);
                         depth += 1;
                         nodes.Insert(depth, ((XDustLogicNode)node).CurrentBody);
                     }
                     else
                     {
                         var root = (IXDustSectionNode)nodes[depth - 1].Last();
                         root.EndBody();
                         nodes[depth] = root.StartBody(tagName);
                     }
                 }
                 else if (op == '/')
                 {
                     depthChange = true;
                     if (depth > 0)
                     {
                         nodes.RemoveAt(depth);
                         depth -= 1;
                     }
                 }
             }
             if (!selfClosed && null != node && !depthChange)
             {
                 nodes[depth].Add(node);
             }
         }
         else
         {
             tagParts = tagParts[0].Split('|');
             IEnumerable<String> filters = tagParts.Skip(1);
             tag = tagParts[0];
             node = new XDustVariableNode(tag, filters);
         }
         if (null != node && !depthChange)
         {
             nodes[depth].Add(node);
         }
         match = match.NextMatch();
     }
     var tail = str.Substring(lastEnd);
     if (!String.IsNullOrEmpty(tail))
     {
         nodes[depth].Add(new XDustTextNode(tail));
     }
     return nodes[0];
 }
示例#6
0
 public virtual String RenderBody(XDust dust, String name, RenderChain chain, Context context, Object model)
 {
     StringBuilder sb = new StringBuilder();
     XDustNodeList body = this.Bodies.ContainsKey(name) ? this.Bodies[name] : null;
     context = new Context(context, model, this.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as Object));
     if (null != this.Scope)
     {
         model = this.Scope.Resolve(context, model);
     }
     else if (null != this.Context)
     {
         model = this.Context.Resolve(context, model);
     }
     if (null != body)
     {
         chain = new RenderChain(chain, body);
         if (this.AllowIteration && (model is IEnumerable<Object> || (model is Scriptable && ((Scriptable)model).IsEnumerable)))
         {
             var list = model is IEnumerable<Object> ? (IEnumerable<Object>)model : ((Scriptable)model).AsEnumerable;
             var length = list.Count();
             if (name == XDustLogicNode.ELSE && length < 1)
             {
                 sb.Append(body.Render(dust, chain, context, null));
             }
             else
             {
                 for (var i = 0; i < length; i++)
                 {
                     Context iterModel = this.PrepareModel(dust, chain, context, list.ElementAt(i));
                     iterModel.Parameters["@idx"] = i;
                     iterModel.Parameters["@sep"] = i != length - 1;
                     sb.Append(body.Render(dust, chain, context, iterModel));
                 }
             }
         }
         else
         {
             Context iterModel = this.PrepareModel(dust, chain, context, model);
             sb.Append(body.Render(dust, chain, context, iterModel));
         }
     }
     return sb.ToString();
 }
示例#7
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     chain = new RenderChain(chain, this);
     context = new Context(context, null, this.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as Object));
     var bodyName = this.ChooseBodyName(context, model);
     return this.RenderBody(dust, bodyName, chain, context, model);
 }
示例#8
0
 public virtual Context PrepareModel(XDust dust, RenderChain chain, Context context, Object model)
 {
     var parameters = new Dictionary<String, Object>();
     if (null != this.Parameters)
     {
         foreach (var kvp in this.Parameters)
         {
             parameters[kvp.Key] = kvp.Value.Render(dust, chain, context, context);
         }
     }
     Context result = new Context(context, model, parameters);
     return result;
 }
示例#9
0
 public abstract String Render(XDust dust, RenderChain chain, Context context, Object model);
示例#10
0
 public override Context PrepareModel(XDust dust, RenderChain chain, Context context, object model)
 {
     return new Context(context, context, this.Parameters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as Object));
 }
示例#11
0
 public String Render(XDust dust, RenderChain chain, Context context, Object model)
 {
     return this.RootNode.Render(dust, chain, context, model);
 }
示例#12
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     return String.Empty;
 }
 public override String Render(XDust dust, RenderChain chain, Context context, object model)
 {
     return this.Character;
 }
示例#14
0
 public override string Render(XDust dust, RenderChain chain, Context context, object model)
 {
     this.Close();
     return this.Value;
 }