Esempio n. 1
0
        public override String Render(Dictionary <String, Object> args)
        {
            StringBuilder sb = new StringBuilder();

            Match m = Regex.Match(Content, Constants.FOR_BEGIN_REGEX);

            if (m.Success)
            {
                String variable = GetVariable(m.Value);
                if (args.ContainsKey(variable) && args[variable] is List <Dictionary <String, Object> > )
                {
                    List <Dictionary <String, Object> > loopArgs = args[variable] as List <Dictionary <String, Object> >;
                    int    firstIndex = Content.IndexOf(m.Value);
                    String endFor     = String.Format(@"$endfor{0}{1}{2}", "{", variable, "}");
                    int    lastIndex  = Content.LastIndexOf(endFor) + endFor.Length;
                    if (firstIndex >= 0 && lastIndex >= 0)
                    {
                        if (firstIndex >= 0)
                        {
                            SimpleTemplate prevFor = new SimpleTemplate(Content.Substring(0, firstIndex));
                            sb.Append(prevFor.Render(args));
                        }
                        String loopBody = RemoveForTag(Content.Substring(firstIndex, lastIndex - firstIndex), variable);
                        foreach (var argItem in loopArgs)
                        {
                            if (Regex.IsMatch(loopBody, Constants.FOR_BEGIN_REGEX))
                            {
                                LoopTemplate lt = new LoopTemplate(loopBody);
                                sb.Append(lt.Render(argItem));
                            }
                            else
                            {
                                SimpleTemplate st = new SimpleTemplate(loopBody);
                                sb.Append(st.Render(argItem));
                            }
                        }
                        if (lastIndex >= 0)
                        {
                            SimpleTemplate afterFor = new SimpleTemplate(Content.Substring(lastIndex));
                            sb.Append(afterFor.Render(args));
                        }
                    }
                }
            }
            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 解析include模版
        /// </summary>
        /// <param name="tpl"></param>
        /// <returns></returns>
        protected String AnalyzeTemplate(String tpl, Dictionary <String, Object> args)
        {
            String          result = tpl;
            MatchCollection mc     = Regex.Matches(result, Constants.INCLUDE_REGEX);

            foreach (Match m in mc)
            {
                String         variable = GetVariable(m.Value);
                SimpleTemplate template = TemplateFactory.GetTemplate <SimpleTemplate>(variable);
                String         value    = null;
                if (template != null)
                {
                    value = template.Render(args);
                }
                else
                {
                    value = String.Format(" [can't find template \"{0}\"] ", variable);
                }
                result = result.Replace(m.Value, value);
            }
            return(result);
        }