コード例 #1
0
        public string ExecuteString(string codeTemplate, IContent content, IDictionary <string, object> tokens = null)
        {
            if (!string.IsNullOrEmpty(codeTemplate))
            {
                var config = new TemplateServiceConfiguration();
#if DEBUG
                config.Debug = true;
#endif
                string result = "";
                using (var service = RazorEngineService.Create(config)) {
                    var model = new RazorModelContext {
                        OrchardServices = _orchardServices,
                        ContentItem     = content,
                        Tokens          = tokens ?? new Dictionary <string, object>(),
                        T = T
                    };
                    result = service.RunCompile(new LoadedTemplateSource(codeTemplate, null), "htmlRawTemplatea", null, (Object)model);
                }
                string resultnobr = result.Replace("\r\n", "").Replace(" ", "");
                if (!string.IsNullOrEmpty(resultnobr))
                {
                    return(result);
                }
            }
            return(null);
        }
コード例 #2
0
        public string RunFile(string localFilePath, RazorModelContext model)
        {
            string key = localFilePath;

            /*
             * if (File.Exists(Path.GetFullPath(localFilePath) + "Reset.txt")) {
             *    StartNewRazorEngine();
             *    File.Delete(Path.GetFullPath(localFilePath) + "Reset.txt");
             * }
             */
            if (File.Exists(localFilePath))
            {
                DateTime d = System.IO.File.GetLastWriteTime(localFilePath);
                key += d.ToShortDateString() + d.ToLongTimeString();
            }
            string codeTemplate = "";

            if (!RazorEngineServiceStatic.IsTemplateCached(key, null))
            {
                if (System.IO.File.Exists(localFilePath))
                {
                    codeTemplate = File.ReadAllText(localFilePath);
                    if (!string.IsNullOrEmpty(codeTemplate))
                    {
                        RazorEngineServiceStatic.AddTemplate(key, new LoadedTemplateSource(codeTemplate, localFilePath));
                        RazorEngineServiceStatic.Compile(key, null);
                        listCached.Add(localFilePath);
                    }
                    else
                    {
                        return("");
                    }
                }
                else
                {
                    return("");
                }
            }
            string result = "";

#if DEBUG
            codeTemplate = File.ReadAllText(localFilePath);
            result       = RazorEngineServiceStatic.RunCompile(new LoadedTemplateSource(codeTemplate, localFilePath), Guid.NewGuid().ToString(), null, (Object)model);
            // non uso la cache in modo da permettere il debug anche nel caso in cui il razor venga caricato più volte
#else
            result = RazorEngineServiceStatic.Run(key, null, (Object)model);
#endif
            string resultnobr = (result ?? "").Replace("\r\n", "").Replace(" ", "");
            if (!string.IsNullOrEmpty(resultnobr))
            {
                return(result);
            }
            else
            {
                return("");
            }
        }
コード例 #3
0
        public string Execute(string codeFileWithExtension, IContent content, IDictionary <string, object> tokens = null)
        {
            string localFile = _razorService.CalculateFallbackTenantCodePosition("Code", codeFileWithExtension);
            var    model     = new RazorModelContext {
                OrchardServices = _orchardServices,
                ContentItem     = content,
                Tokens          = tokens ?? new Dictionary <string, object>(),
                T = T
            };

            //           RazorTemplateManager rt = new RazorTemplateManager();
            return(_razorTemplateManager.RunFile(localFile, model));
        }