예제 #1
0
        public void PrefixFromSettings()
        {
            var settings = new SparkSettings()
                           .SetPageBaseType(typeof(StubSparkView))
                           .SetPrefix("s");

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = new FileSystemViewFolder("Spark.Tests.Views")
            };

            var view = (StubSparkView)engine.CreateInstance(new SparkViewDescriptor().AddTemplate("Prefix\\prefix-from-settings.spark"));

            view.ViewData["Names"] = new[] { "alpha", "beta", "gamma" };

            var output = new StringWriter();

            view.RenderView(output);

            ContainsInOrder(output.ToString(),
                            "<li", "alpha", "</li>",
                            "<li", "beta", "</li>",
                            "<li", "gamma", "</li>",
                            "<var x=\"1/0\">element ignored</var>",
                            "<p each=\"5\">attribute ignored</p>"
                            );
        }
예제 #2
0
        public void SectionAsSegmentAndRenderPrefixes()
        {
            var settings = new SparkSettings()
                           .SetPageBaseType(typeof(StubSparkView))
                           .SetParseSectionTagAsSegment(true);

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = new FileSystemViewFolder("Spark.Tests.Views")
            };

            var view =
                (StubSparkView)
                engine.CreateInstance(new SparkViewDescriptor().AddTemplate("Prefix\\section-render-prefix.spark"));
            var output = new StringWriter();

            view.RenderView(output);

            ContainsInOrder(output.ToString(),
                            "<p>one</p>",
                            "<p>two</p>",
                            "<p>three</p>",
                            "<macro:ignored>ignored</macro:ignored>",
                            "<content:ignored>ignored</content:ignored>",
                            "<use:ignored>ignored</use:ignored>",
                            "<render:ignored>ignored</render:ignored>",
                            "<section:ignored>ignored</section:ignored>"
                            );
        }
예제 #3
0
        public string Convert(string templateName, ViewData data, string relativeOutputPath)
        {
            string template = templateName;

            if (template.StartsWith(templatePath))
            {
                template = template.Substring(templatePath.Length + 1);
            }

            SparkViewDescriptor descriptor = new SparkViewDescriptor()
                                             .AddTemplate(template);
            var view = (SparkTemplateBase)engine.CreateInstance(descriptor);

            using (var writer = new StringWriter())
            {
                try
                {
                    view.RelativeOutputPath = relativeOutputPath;
                    view.ViewData           = data;
                    view.RenderView(writer);
                }
                finally
                {
                    engine.ReleaseInstance(view);
                }

                return(writer.ToString());
            }
        }
예제 #4
0
파일: SparkRender.cs 프로젝트: haf/Topshelf
        public string Render <TViewData>(string template, TViewData data)
        {
            var settings = new SparkSettings();

            settings.AddNamespace("Topshelf.Dashboard");
            settings.PageBaseType = typeof(TopshelfView).FullName;

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = _viewFolder,
            };

            ISparkView instance = engine.CreateInstance(new SparkViewDescriptor().AddTemplate(template));

            var view = (TopshelfView <TViewData>)instance;

            view.SetModel(data);

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
                view.RenderView(writer);

            return(sb.ToString());
        }
예제 #5
0
        public static string RenderTemplate(string template, ViewDataDictionary viewData)
        {
            // Set up your spark engine goodness.
            var settings = new SparkSettings().SetPageBaseType(typeof(SparkView));

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = new FileSystemViewFolder(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\IntegrationTestingViews\Views")
            };

            // "Describe" the view (the template, it is a template after all), and its details.
            var descriptor = new SparkViewDescriptor().AddTemplate(template);

            // Create a spark view engine instance
            var view = (SparkView)engine.CreateInstance(descriptor);

            try
            {
                // Merge the view data.
                viewData.Keys.ToList().ForEach(x => view.ViewData[x] = viewData[x]);

                // Render the view to a text writer.
                var writer = new StringWriter();
                view.RenderView(writer);
                return(writer.ToString());
            }
            finally
            {
                engine.ReleaseInstance(view);
            }
        }
예제 #6
0
        public void UseAssemblyAndNamespaceFromSettings()
        {
            var settings = new SparkSettings()
                           .AddNamespace("System.Web")
                           .AddAssembly("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
                           .SetPageBaseType(typeof(StubSparkView));

            var views = new InMemoryViewFolder
            {
                { Path.Combine("home", "index.spark"), "<div>${ProcessStatus.Alive}</div>" }
            };

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = views
            };

            var descriptor = new SparkViewDescriptor();

            descriptor.Templates.Add(Path.Combine("home", "index.spark"));

            var contents = engine.CreateInstance(descriptor).RenderView();

            Assert.AreEqual("<div>Alive</div>", contents);
        }
예제 #7
0
    static void Main(string[] args)
    {
        // following are one-time steps
        // create engine
        var settings = new SparkSettings()
                       .SetPageBaseType(typeof(EmailView));
        var templates = new InMemoryViewFolder();
        var engine    = new SparkViewEngine(settings)
        {
            ViewFolder = templates
        };

        // add templates
        templates.Add("sample.spark", @"Dear ${user.Name}, This is an email.Sincerely, Spark View Engine http://constanto.org/unsubscribe/${user.Id}");
        // following are per-render steps
        // render template
        var descriptor = new SparkViewDescriptor()
                         .AddTemplate("sample.spark");
        var view = (EmailView)engine.CreateInstance(descriptor);

        view.user = new User {
            Id = 655321, Name = "Alex"
        };
        view.RenderView(Console.Out);
        Console.ReadLine();
    }
예제 #8
0
        public void PrefixFromXmlns()
        {
            var view = (StubSparkView)_engine.CreateInstance(new SparkViewDescriptor().AddTemplate("Prefix\\prefix-from-xmlns.spark"));

            view.ViewData["Names"] = new[] { "alpha", "beta", "gamma" };

            var output = new StringWriter();

            view.RenderView(output);

            ContainsInOrder(output.ToString(),
                            "<li", "alpha", "</li>",
                            "<li", "beta", "</li>",
                            "<li", "gamma", "</li>",
                            "<var x=\"1/0\">element ignored</var>",
                            "<p each=\"5\">attribute ignored</p>");
        }
예제 #9
0
        private ISettableModel GetView(Type modelType)
        {
            var modelName = modelType.Name;
            var viewName  = String.Format("{0}.spark", modelName.Replace("ViewModel", ""));

            var descriptor = _builder.Build(viewName);
            var entry      = _engine.CreateInstance(descriptor);

            return((ISettableModel)entry);
        }
예제 #10
0
        private ISparkView CreateView(IViewFolder viewFolder, string template)
        {
            var settings = new SparkSettings().SetPageBaseType(typeof(StubSparkView));

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = viewFolder
            };

            return(engine.CreateInstance(new SparkViewDescriptor().AddTemplate(template)));
        }
예제 #11
0
        public void InMemoryViewFolderUsedByEngine()
        {
            var folder = new InMemoryViewFolder();
            folder.Add("home\\index.spark", "<p>Hello world</p>");
            var engine = new SparkViewEngine(new SparkSettings().SetPageBaseType(typeof (StubSparkView))){ViewFolder = folder};

            var descriptor = new SparkViewDescriptor();
            descriptor.Templates.Add("home\\index.spark");
            var view = engine.CreateInstance(descriptor);
            var contents = view.RenderView();
            Assert.AreEqual("<p>Hello world</p>", contents);
        }
예제 #12
0
        public IBuilder Load(Options options)
        {
            var currentDirectory  = Environment.CurrentDirectory;
            var assemblyDirectory = Path.GetDirectoryName(typeof(SakeEngine).Assembly.Location);

            var settings = new SparkSettings()
                           .SetPageBaseType(typeof(BuilderBase))
                           .SetAutomaticEncoding(true)
                           .SetAttributeBehaviour(AttributeBehaviour.TextOriented)
                           .SetDebug(true);

            IViewFolder viewFolder = new FileSystemViewFolder(currentDirectory);

            foreach (var includeDir in options.IncludeDirectory)
            {
                var path = Path.Combine(currentDirectory, includeDir);
                viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(path));
                foreach (var file in Directory.EnumerateFiles(path, "*.dll"))
                {
                    try
                    {
                        Assembly.LoadFile(file);
                    }
                    catch
                    {
                    }
                }
            }

            viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(assemblyDirectory));

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder       = viewFolder,
                ExtensionFactory = new ExtensionFactory(),
            };

            var descriptor = new SparkViewDescriptor
            {
                Templates = new[] { options.Makefile }
            };

            var builder = (BuilderBase)engine.CreateInstance(descriptor);

            builder.Output       = new StringWriter();
            builder.Log          = _log;
            builder.SakeSettings = _settings;
            builder.Render();
            return(builder);
        }
예제 #13
0
        public void CustomViewActivator()
        {
            var engine = new SparkViewEngine(
                new SparkSettings().SetPageBaseType(typeof(StubSparkView)))
                             {
                                 ViewActivatorFactory = new CustomFactory(),
                                 ViewFolder = new InMemoryViewFolder { { "hello/world.spark", "<p>hello world</p>" } }
                             };

            var descriptor = new SparkViewDescriptor().AddTemplate("hello/world.spark");
            var view = engine.CreateInstance(descriptor);

            Assert.IsNotNull(view);
            Assert.IsAssignableFrom(typeof(TestView), view);
        }
예제 #14
0
        public void Render(Template template, object data, TextWriter output)
        {
            var descriptor = new SparkViewDescriptor().AddTemplate(template.Name + ".spark");

            var view = (ViewBase)_engine.CreateInstance(descriptor);

            try
            {
                view.Data = data;
                view.RenderView(output);
            }
            finally
            {
                _engine.ReleaseInstance(view);
            }
        }
예제 #15
0
        public T CreateFrom <T>()
        {
            var settings = new SparkSettings().SetPageBaseType(typeof(T));

            var templates = new FileSystemViewFolder(@"C:\Two to Tango\Internal\Labs\BlogAnonymousObjectAsModelForSparkView\WebApplication1\Templates");

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = templates
            };

            var descriptor = new SparkViewDescriptor();

            descriptor.AddTemplate("test.sprk.htm");

            return((T)engine.CreateInstance(descriptor));
        }
예제 #16
0
        public void CustomViewActivator()
        {
            var engine = new SparkViewEngine(
                new SparkSettings().SetPageBaseType(typeof(StubSparkView)))
            {
                ViewActivatorFactory = new CustomFactory(),
                ViewFolder           = new InMemoryViewFolder {
                    { "hello/world.spark", "<p>hello world</p>" }
                }
            };

            var descriptor = new SparkViewDescriptor().AddTemplate("hello/world.spark");
            var view       = engine.CreateInstance(descriptor);

            Assert.IsNotNull(view);
            Assert.IsAssignableFrom(typeof(TestView), view);
        }
예제 #17
0
        public string Populate(string templateFilePath, object data)
        {
            var writer = new StringWriter();

            var descriptor = new SparkViewDescriptor();

            descriptor.AddTemplate(templateFilePath);
            var view = (TemplateBase)engine.CreateInstance(descriptor);

            try {
                view.ViewData = new ViewDataDictionary(data);
                view.RenderView(writer);
            } finally {
                engine.ReleaseInstance(view);
            }
            return(writer.ToString());
        }
        public void UsingCSharpSyntaxInsideEngine()
        {
            // engine takes base class and IViewFolder
            var engine = new SparkViewEngine(
                new SparkSettings().SetPageBaseType("Spark.Tests.Stubs.StubSparkView")) { SyntaxProvider = _syntax, ViewFolder = new FileSystemViewFolder("Spark.Tests.Views") };

            // describe and instantiate view
            var descriptor = new SparkViewDescriptor();
            descriptor.Templates.Add("Code\\simplecode.spark");
            var view = (StubSparkView)engine.CreateInstance(descriptor);

            // provide data and render
            view.ViewData["hello"] = "world";
            var code = view.RenderView();

            Assert.IsNotNull(code);
        }
예제 #19
0
        public void InMemoryViewFolderUsedByEngine()
        {
            var folder = new InMemoryViewFolder();

            folder.Add(Path.Combine("home", "index.spark"), "<p>Hello world</p>");
            var engine = new SparkViewEngine(new SparkSettings().SetPageBaseType(typeof(StubSparkView)))
            {
                ViewFolder = folder
            };

            var descriptor = new SparkViewDescriptor();

            descriptor.Templates.Add(Path.Combine("home", "index.spark"));
            var view     = engine.CreateInstance(descriptor);
            var contents = view.RenderView();

            Assert.AreEqual("<p>Hello world</p>", contents);
        }
예제 #20
0
        public ITemplate Compile <T>(string template)
        {
            SparkViewEngine _engine = new SparkViewEngine {
                ViewFolder = new FileSystemViewFolder(HttpContext.Current.Request.PhysicalApplicationPath), DefaultPageBaseType = string.Format("Nina.ViewEngines.Spark.DataView<{0}>", typeof(T).FullName)
            };

            SparkViewDescriptor vds = new SparkViewDescriptor();


            vds.AddTemplate(template + ".spark");
            var layout = DetectLayout(template, _engine.ViewFolder);

            if (!string.IsNullOrEmpty(layout))
            {
                vds.AddTemplate(layout);
            }
            var sparkView = (DataView <T>)_engine.CreateInstance(vds);

            return(new SparkTemplate(sparkView));
        }
예제 #21
0
        public void StatementAndExpressionInCode()
        {
            // engine takes base class and IViewFolder
            var engine = new SparkViewEngine(
                new SparkSettings().SetPageBaseType("Spark.Tests.Stubs.StubSparkView"))
            {
                SyntaxProvider = _syntax, ViewFolder = new FileSystemViewFolder("Spark.Tests.Views")
            };

            // describe and instantiate view
            var descriptor = new SparkViewDescriptor();

            descriptor.Templates.Add(Path.Combine("Code", "foreach.spark"));
            var view = (StubSparkView)engine.CreateInstance(descriptor);

            // provide data and render
            view.ViewData["hello"] = "world";
            var code = view.RenderView();

            Assert.IsNotNull(code);
        }
예제 #22
0
        public string Render(string template, ValidationVote[] votes, KlingerInformation info)
        {
            var view = (VoteView)_engine.CreateInstance(
                new SparkViewDescriptor()
                .AddTemplate(template));

            view.Model = new HealthModel
            {
                Votes       = votes,
                Information = info
            };

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                view.RenderView(writer);
            }

            return(sb.ToString());
        }
예제 #23
0
        public void Each(Action <dynamic> action)
        {
            string        viewPath;
            FileInfo      template;
            DirectoryInfo directoryInfo;

            if (_viewPath != null)
            {
                template      = new FileInfo(_viewPath);
                viewPath      = template.DirectoryName;
                directoryInfo = new DirectoryInfo(viewPath);
            }
            else
            {
                viewPath      = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "views");
                directoryInfo = new DirectoryInfo(viewPath);
                template      = GetDefaultTemplateFile(directoryInfo);
            }

            var typeName = typeof(SparkView).FullName;

            if (_model != null)
            {
                Type modelType = _model.GetType();
                //var viewType = typeof(SparkView<>).MakeGenericType(modelType);
                typeName = "Tabasco.TemplateEngines.SparkView<" + modelType.FullName + ">";
            }

            var engine = new SparkViewEngine
            {
                DefaultPageBaseType = typeName,
                ViewFolder          = new FileSystemViewFolder(directoryInfo.FullName)
            };


            var descriptor   = new SparkViewDescriptor().AddTemplate(template.Name);
            var viewInstance = engine.CreateInstance(descriptor);

            dynamic view = viewInstance;

            if (_model != null && typeName != null)
            {
                //view = Convert.ChangeType(viewInstance, Type.GetType(typeof(SparkView<>).MakeGenericType(_model.GetType)));
                view.Model = _model;
            }

            var stream = new MemoryStream();
            var writer = new StreamWriter(stream, Encoding.UTF8);

            view.RenderView(writer);

            writer.Flush();

            var reader = new StreamReader(stream);

            stream.Position = 0;

            var viewString = reader.ReadToEnd();

            action(viewString);

            writer.Dispose();
            reader.Dispose();
            stream.Dispose();
        }
예제 #24
0
        public void UseAssemblyAndNamespaceFromSettings()
        {
            var settings = new SparkSettings()
                .AddNamespace("System.Web")
                .AddAssembly("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
                .SetPageBaseType(typeof(StubSparkView));

            var views = new InMemoryViewFolder();
            views.Add("Home\\Index.spark", "<div>${ProcessStatus.Alive}</div>");

            var engine = new SparkViewEngine(settings) {ViewFolder = views};

            var descriptor = new SparkViewDescriptor();
            descriptor.Templates.Add("home\\index.spark");

            var contents = engine.CreateInstance(descriptor).RenderView();
            Assert.AreEqual("<div>Alive</div>", contents);
        }
예제 #25
0
        public void SectionAsSegmentAndRenderPrefixes()
        {
            var settings = new SparkSettings()
                .SetPageBaseType(typeof (StubSparkView))
                .SetParseSectionTagAsSegment(true);

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = new FileSystemViewFolder("Spark.Tests.Views")
            };

            var view =
                (StubSparkView)
                engine.CreateInstance(new SparkViewDescriptor().AddTemplate("Prefix\\section-render-prefix.spark"));
            var output = new StringWriter();
            view.RenderView(output);

            ContainsInOrder(output.ToString(),
                            "<p>one</p>",
                            "<p>two</p>",
                            "<p>three</p>",
                            "<macro:ignored>ignored</macro:ignored>",
                            "<content:ignored>ignored</content:ignored>",
                            "<use:ignored>ignored</use:ignored>",
                            "<render:ignored>ignored</render:ignored>",
                            "<section:ignored>ignored</section:ignored>"
                );
        }
예제 #26
0
        public void PrefixFromSettings()
        {
            var settings = new SparkSettings()
                .SetPageBaseType(typeof(StubSparkView))
                .SetPrefix("s");

            var engine = new SparkViewEngine(settings)
                             {
                                 ViewFolder = new FileSystemViewFolder("Spark.Tests.Views")
                             };

            var view = (StubSparkView)engine.CreateInstance(new SparkViewDescriptor().AddTemplate("Prefix\\prefix-from-settings.spark"));
            view.ViewData["Names"] = new[] { "alpha", "beta", "gamma" };

            var output = new StringWriter();
            view.RenderView(output);

            ContainsInOrder(output.ToString(),
                            "<li", "alpha", "</li>",
                            "<li", "beta", "</li>",
                            "<li", "gamma", "</li>",
                            "<var x=\"1/0\">element ignored</var>",
                            "<p each=\"5\">attribute ignored</p>"
                );
        }
예제 #27
0
        private ISparkView CreateView(IViewFolder viewFolder, string template)
        {
            CompiledViewHolder.Current = new CompiledViewHolder();

            var settings = new SparkSettings().SetPageBaseType(typeof(StubSparkView));

            var engine = new SparkViewEngine(settings)
                             {
                                 ViewFolder = viewFolder
                             };

            return engine.CreateInstance(new SparkViewDescriptor().AddTemplate(template));
        }
예제 #28
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Write(@"
Transforms Xml using a Spark template

XPARK templatefile [inputfile [outputfile]]

  templatefile  Path to a .spark file.
  inputfile     Source xml. Path to an file or url for http GET.
  outputfile    Target file to receive template output.

If inputfile or outputfile are not provided stdin and stdout are used.

The Model in the template is an XDocument loaded from the source.

The templatefile location may also contain _partial.spark files, and
a _global.spark file with common namespaces, macros, etc.
");
                return;
            }

            // Find the full path to the template file,
            // using current directory if argument isn't fully qualified
            var templatePath    = Path.Combine(Environment.CurrentDirectory, args[0]);
            var templateName    = Path.GetFileName(templatePath);
            var templateDirPath = Path.GetDirectoryName(templatePath);

            var viewFolder = new FileSystemViewFolder(templateDirPath);

            // Create an engine using the templates path as the root location
            // as well as the shared location
            var engine = new SparkViewEngine
            {
                DefaultPageBaseType = typeof(SparkView).FullName,
                ViewFolder          = viewFolder.Append(new SubViewFolder(viewFolder, "Shared"))
            };

            SparkView view;

            // compile and instantiate the template
            view = (SparkView)engine.CreateInstance(
                new SparkViewDescriptor()
                .AddTemplate(templateName));


            // load the second argument, or default to reading stdin
            if (args.Length >= 2)
            {
                view.Model = XDocument.Load(args[1]);
            }
            else
            {
                view.Model = XDocument.Load(XmlReader.Create(Console.OpenStandardInput()));
            }


            // write out to the third argument, or default to writing stdout
            if (args.Length >= 3)
            {
                using (var writer = new StreamWriter(new FileStream(args[2], FileMode.Create), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
            else
            {
                using (var writer = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
        }