Exemple #1
0
        public void Render()
        {
            var         scriptEngine = IronRuby.GetEngine(_scriptRuntime);
            ScriptScope scope        = scriptEngine.CreateScope();

            scope.SetVariable("output_buffer", _output);

            TemplateParser = new RubyTemplateParser(ViewSource);
            TemplateParser.AddRequire("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

            StringBuilder script      = new StringBuilder();
            var           methodNames = new Stack <string>();

            BuildScript(script, methodNames);
            var methodsCounter = methodNames.Count;

            while (methodNames.Count > 0)
            {
                script.Append(methodNames.Pop());
                if (methodNames.Count > 0)
                {
                    script.Append(" { ");
                }
            }
            if (methodsCounter > 1)
            {
                script.Append(new String('}', methodsCounter - 1));
            }
            script.AppendLine();

            try
            {
                Debug.WriteLine(script.ToString());
                ScriptSource source = scriptEngine.CreateScriptSourceFromString(script.ToString());
                source.Execute(scope);
            }
            catch (Exception e)
            {
                throw new MonoRailException(script.ToString(), e);
            }
            finally
            {
                // TODO : check if remove variable isnt faster.
                scope.ClearVariables();
                scriptEngine.Shutdown();
            }
        }