Beispiel #1
0
        internal string BuildNJangoPage(string workingDirectory, string templateFile, IronPython.Runtime.PythonDictionary pyContext)
        {
            TemplateManagerProvider templateProvider = new TemplateManagerProvider();
            ITemplateManager        manager          = templateProvider.GetNewManager();

            Dictionary <string, object> context = new Dictionary <string, object>();

            foreach (object str in pyContext.Keys)
            {
                context.Add(str.ToString(), pyContext[str]);
            }

            if (File.Exists(workingDirectory + @"\" + templateFile.ToString()))
            {
                TextReader reader = manager.RenderTemplate(
                    workingDirectory + @"\" + templateFile.ToString(),
                    (Dictionary <string, Object>)context);

                return(reader.ReadToEnd());
            }
            else
            {
                return("Template not found");
            }
        }
Beispiel #2
0
        private static string Render(string template, IDictionary <string, object> values, ITemplateManager templateManager)
        {
            lock (Error)
            {
                string exceptionMessage = null;
                string replacementValue;

                try
                {
                    Error.Invoked    = false;
                    replacementValue = templateManager.RenderTemplate(StringProvider + template, values).ReadToEnd();
                }
                catch (Exception ex)
                {
                    exceptionMessage = $"\n{ex.Message}";
                    replacementValue = string.Empty;
                    Error.Invoked    = true;
                }

                if (Error.Invoked)
                {
                    if (string.IsNullOrWhiteSpace(replacementValue) || !replacementValue.Contains(Error.ToString()))
                    {
                        throw new ArgumentException(string.Format("Tag substitution errored on template string:\n{0}{1}", template, exceptionMessage));
                    }

                    var attemptedRender = replacementValue.Replace(Error.ToString(), "[ERROR OCCURRED HERE]");
                    throw new ArgumentException(string.Format("Tag substitution failed on template string:\n{0}\n\nAttempted rendering was:\n{1}{2}", template, attemptedRender, exceptionMessage));
                }

                return(replacementValue);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Processes the view - using the templateName
        /// to obtain the correct template
        /// and writes the results to the System.IO.TextWriter.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="context"></param>
        /// <param name="controller"></param>
        /// <param name="templateName"></param>
        public override void Process(System.IO.TextWriter output, IRailsEngineContext context, Controller controller, string templateName)
        {
            HttpApplicationState app = context.UnderlyingContext.Application;

            // If there's no manager - managerProvider will return new one for us.
            if (app[cDjangoManagerKey] == null)
            {
                // Since one HttpApplication processed by a single thread - we don't need no locking here.
                app[cDjangoManagerKey] = managerProvider.GetNewManager();
            }

            ITemplateManager mgr = app[cDjangoManagerKey] as ITemplateManager;

            if (mgr == null)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error("Couldn't get ITemplateManager from the HttpApplicationState");
                }

                throw new RailsException("Couldn't get ITemplateManager from the HttpApplicationState");
            }


            AdjustContentType(context);

            string resolvedName = Path.HasExtension(templateName) ? templateName : templateName + cTemplateExtension;

            try
            {
                var djangoContext = CreateContext(context, controller);

                TextReader reader = mgr.RenderTemplate(resolvedName, djangoContext);
                char[]     buffer = new char[4096];
                int        count  = 0;
                while ((count = reader.ReadBlock(buffer, 0, 4096)) > 0)
                {
                    output.Write(buffer, 0, count);
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error("Could not render view", ex);
                }

                throw new RailsException("Could not render view: " + resolvedName, ex);
            }
        }
Beispiel #4
0
        public void Render <T>(TextWriter output, T data)
        {
            _dict.Clear();
            _dict["ViewData"] = data;
            _dict["Model"]    = data;

            var reader = _manager.RenderTemplate(_template, _dict);

            char[] buffer = new char[4096];
            int    count  = 0;

            while ((count = reader.ReadBlock(buffer, 0, 4096)) > 0)
            {
                output.Write(buffer, 0, count);
            }
        }
Beispiel #5
0
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            data_dictionary = viewContext.ViewData;

            var requestContext = new Dictionary <string, object>(viewContext.ViewData);

            requestContext["Html"]    = new HtmlHelper(viewContext, this);
            requestContext["Model"]   = viewContext.ViewData.Model;
            requestContext["Session"] = viewContext.HttpContext.Session;

            var reader = iTemplateManager.RenderTemplate(viewPath, requestContext);
            var buffer = new char[4096];
            int count  = 0;

            while ((count = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                writer.Write(buffer, 0, count);
            }
        }