Beispiel #1
0
        public override void Render(TemplateContext context)
        {
            var result = context.Evaluate(_expression);

            if (result == null)
            {
                if (_else != null)
                {
                    _else.Render(context);
                }
            }
            else if (result is IEnumerable)
            {
                var count = Parsing.Operations.GetCount(result);
                if (count > 0)
                {
                    var state = new State(count);

                    var data = new TemplateData();
                    data.Set("forloop", state);
                    data.Set(_variable + "_for", state);

                    context.PushData(data);

                    foreach (object item in ( IEnumerable )result)
                    {
                        data.Set(_variable, item);
                        RenderElements(context);
                        state.Next();
                    }

                    context.PopData();
                }
                else
                {
                    if (_else != null)
                    {
                        _else.Render(context);
                    }
                }
            }
            else
            {
                throw new TagRenderErrorException("For", TemplateErrors.ExpressionNotEnumerable);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            string path = Environment.CurrentDirectory;

            while (!Directory.Exists(Path.Combine(path, "Input")))
            {
                path = Path.GetDirectoryName(path);
            }

            string inputPath  = Path.Combine(path, "Input");
            string outputPath = Path.Combine(path, "Output");

            Template.Global.RegisterPath(inputPath);
            Template.Global.Culture = CultureInfo.InvariantCulture;

            Template.Global.Data.Set("Lang", TemplateTools.ReadStrings(Path.Combine(inputPath, "Strings.txt")));
            Template.Global.QuickData = Template.Global.Data.Get("Lang");

            var data = new TemplateData();

            data.Set("User", Environment.UserName);
            data.Set("Version", Environment.OSVersion.Version);

            var query = from server in new int[] { 10, 20, 50, 51, 52, 53, 54 }
            select new { Server = "Server" + server, Number = server, Status = "OK" };

            data.Set("Servers", query);

            var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady);

            data.Set("Drives", drives);

            foreach (string input in Directory.GetFiles(inputPath, "*.htm"))
            {
                var output   = Path.Combine(outputPath, Path.GetFileName(input));
                var template = Template.FromFile(input, data);
                var result   = template.Render();

                File.WriteAllText(output, result, template.CurrentEncoding);
            }
        }