Beispiel #1
0
        /// <summary>
        /// Gets a Mustache template from it's name
        /// </summary>
        /// <param name="name">The name of the template</param>
        /// <returns>The Mustache template</returns>
        public new MustacheTemplate GetTemplate(string name)
        {
            var provider = Controllers.PatternLabController.Provider ?? new PatternProvider();

            // String any Pattern Parameters from the name then find a pattern who's Partial (e.g. atoms-colors) matches the value
            var pattern = provider.Patterns()
                          .FirstOrDefault(
                p => p.Partial.Equals(name.StripPatternParameters(), StringComparison.InvariantCultureIgnoreCase));

            if (pattern == null)
            {
                return(null);
            }

            // If found, read the contents of the template from disk
            var text   = File.ReadAllText(pattern.FilePath);
            var reader = new StringReader(text);

            // Pass the Pattern Parameters to the template
            var template = new MustacheTemplate(name.ToPatternParameters());

            // Load the contents of the file into the template
            template.Load(reader);

            return(template);
        }
Beispiel #2
0
        private MustacheTemplate LoadTemplate(string virtualPath, Dictionary <string, object> parameters = null)
        {
            var physicalPath = HostingEnvironment.MapPath(virtualPath) ?? string.Empty;
            var serializer   = new JavaScriptSerializer();
            var key          = string.Format("{0}-{1}", virtualPath, serializer.Serialize(parameters));

            if (_controllerContext.HttpContext.Cache[key] != null)
            {
                return((MustacheTemplate)_controllerContext.HttpContext.Cache[key]);
            }

            var embeddedResource = _virtualPathProvider.GetFile(virtualPath) as EmbeddedResource;
            var templateSource   = embeddedResource != null?embeddedResource.ReadAllText() : File.ReadAllText(physicalPath);

            var template = new MustacheTemplate(_parameters);

            template.Load(new StringReader(templateSource));

            _controllerContext.HttpContext.Cache.Insert(key, template,
                                                        embeddedResource != null
                    ? embeddedResource.GetCacheDependency(DateTime.UtcNow)
                    : new CacheDependency(physicalPath));

            return(template);
        }
Beispiel #3
0
        private MustacheTemplate LoadTemplate(string virtualPath, Dictionary <string, object> parameters = null)
        {
            var physicalPath = HostingEnvironment.MapPath(virtualPath) ?? string.Empty;
            var serializer   = new JavaScriptSerializer();
            var key          = string.Format("{0}-{1}", virtualPath, serializer.Serialize(parameters));

            // Check cache for template
            if (_controllerContext.HttpContext.Cache[key] != null)
            {
                return((MustacheTemplate)_controllerContext.HttpContext.Cache[key]);
            }

            // Load from disk or assembly embedded resources if not cached
            var embeddedResource = _virtualPathProvider.GetFile(virtualPath) as EmbeddedResource;
            var templateSource   = embeddedResource != null?embeddedResource.ReadAllText() : File.ReadAllText(physicalPath);

            // Pass contents of file into template along with any pattern parameters
            var template = new MustacheTemplate(_parameters);

            template.Load(new StringReader(templateSource));

            // Cache the found template
            _controllerContext.HttpContext.Cache.Insert(key, template,
                                                        embeddedResource != null
                    ? embeddedResource.CacheDependency(DateTime.UtcNow)
                    : new CacheDependency(physicalPath));

            return(template);
        }
Beispiel #4
0
        public new MustacheTemplate GetTemplate(string name)
        {
            var provider = Controllers.PatternLabController.Provider ?? new PatternProvider();
            var pattern  = provider.Patterns()
                           .FirstOrDefault(
                p => p.Partial.Equals(name.StripPatternParameters(), StringComparison.InvariantCultureIgnoreCase));

            if (pattern == null)
            {
                return(null);
            }

            var text     = File.ReadAllText(pattern.FilePath);
            var reader   = new StringReader(text);
            var template = new MustacheTemplate(name.ToPatternParameters());

            template.Load(reader);

            return(template);
        }
        /// <summary>
        /// Gets a Mustache template from it's name
        /// </summary>
        /// <param name="name">The name of the template</param>
        /// <returns>The Mustache template</returns>
        public new MustacheTemplate GetTemplate(string name)
        {
            var provider = Controllers.PatternLabController.Provider ?? new PatternProvider();

            // String any Pattern Parameters from the name then find a pattern who's Partial (e.g. atoms-colors) matches the value
            var pattern = provider.Patterns()
                .FirstOrDefault(
                    p => p.Partial.Equals(name.StripPatternParameters(), StringComparison.InvariantCultureIgnoreCase));

            if (pattern == null) return null;

            // If found, read the contents of the template from disk
            var text = File.ReadAllText(pattern.FilePath);
            var reader = new StringReader(text);

            // Pass the Pattern Parameters to the template
            var template = new MustacheTemplate(name.ToPatternParameters());

            // Load the contents of the file into the template
            template.Load(reader);

            return template;
        }
Beispiel #6
0
        private MustacheTemplate LoadTemplate(string virtualPath, Dictionary<string, object> parameters = null)
        {
            var physicalPath = HostingEnvironment.MapPath(virtualPath) ?? string.Empty;
            var serializer = new JavaScriptSerializer();
            var key = string.Format("{0}-{1}", virtualPath, serializer.Serialize(parameters));

            // Check cache for template
            if (_controllerContext.HttpContext.Cache[key] != null)
            {
                return (MustacheTemplate)_controllerContext.HttpContext.Cache[key];
            }

            // Load from disk or assembly embedded resources if not cached
            var embeddedResource = _virtualPathProvider.GetFile(virtualPath) as EmbeddedResource;
            var templateSource = embeddedResource != null ? embeddedResource.ReadAllText() : File.ReadAllText(physicalPath);

            // Pass contents of file into template along with any pattern parameters
            var template = new MustacheTemplate(_parameters);
            template.Load(new StringReader(templateSource));

            // Cache the found template
            _controllerContext.HttpContext.Cache.Insert(key, template,
                embeddedResource != null
                    ? embeddedResource.CacheDependency(DateTime.UtcNow)
                    : new CacheDependency(physicalPath));

            return template;
        }