Example #1
0
        public override bool render(InternalContextAdapter context, TextWriter writer, INode node)
        {
            INode bodyNode = null;

            IDictionary componentParams = CreateParametersAndFigureOutBlockNode(context, node);

            if (bodyNodeIndex < node.jjtGetNumChildren())
            {
                bodyNode = node.jjtGetChild(bodyNodeIndex);
            }

            NVelocityViewContextAdapter contextAdapter =
                new NVelocityViewContextAdapter(
                    componentName, context, writer, bodyNode, componentParams);

            IRailsEngineContext railsContext = (IRailsEngineContext)context.Get("context");

            component.Init(railsContext, contextAdapter);

            component.Render();

            if (contextAdapter.ViewToRender != null)
            {
                return(RenderComponentView(context, writer, contextAdapter));
            }

            return(true);
        }
Example #2
0
        private bool RenderComponentView(InternalContextAdapter context,
                                         TextWriter writer, NVelocityViewContextAdapter contextAdapter)
        {
            foreach (DictionaryEntry entry in contextAdapter.ContextVars)
            {
                context.Put(entry.Key.ToString(), entry.Value);
            }

            try
            {
                String viewToRender = contextAdapter.ViewToRender;

                viewToRender = String.Format("{0}.vm", viewToRender);

                CheckTemplateStack(context);

                String encoding = SetUpEncoding(context);

                Template template = GetTemplate(viewToRender, encoding);

                return(RenderView(context, viewToRender, template, writer));
            }
            finally
            {
                foreach (DictionaryEntry entry in contextAdapter.ContextVars)
                {
                    context.Remove(entry.Key);
                }
            }
        }
        protected override void ProcessSubSections(ViewComponent component, NVelocityViewContextAdapter contextAdapter)
        {
            foreach (SubSectionDirective section in sectionsCreated)
            {
                if (!component.SupportsSection(section.Name))
                {
                    throw new ViewComponentException(
                              String.Format("The section '{0}' is not supported by the ViewComponent '{1}'",
                                            section.Name, component.Context.ComponentName));
                }

                contextAdapter.RegisterSection(section);
                section.ContextAdapter = contextAdapter;
            }
        }
		protected override void ProcessSubSections(ViewComponent component, NVelocityViewContextAdapter contextAdapter)
		{
			foreach(SubSectionDirective section in sectionsCreated)
			{
				if (!component.SupportsSection(section.Name))
				{
					throw new ViewComponentException(
						String.Format("The section '{0}' is not supported by the ViewComponent '{1}'",
							section.Name, component.Context.ComponentName));
				}

				contextAdapter.RegisterSection(section);
				section.ContextAdapter = contextAdapter;
			}
		}
Example #5
0
        public bool RenderComponentView(IInternalContextAdapter context,
                                        String viewToRender, TextWriter writer,
                                        NVelocityViewContextAdapter contextAdapter)
        {
            if (!viewToRender.ToLower().EndsWith(NVelocityViewEngine.TemplateExtension))
            {
                viewToRender = viewToRender + NVelocityViewEngine.TemplateExtension;
            }

            CheckTemplateStack(context);

            var encoding = ExtractEncoding(context);

            var template = GetTemplate(viewToRender, encoding);

            return(RenderView(context, viewToRender, template, writer));
        }
		public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node)
		{
			IEngineContext railsContext = EngineContextLocator.Instance.LocateCurrentContext();
			IViewComponentRegistry registry = railsContext.Services.GetService<IViewComponentFactory>().Registry;
			IViewComponentDescriptorProvider viewDescProvider =
				railsContext.Services.GetService<IViewComponentDescriptorProvider>();
			ICacheProvider cacheProvider = railsContext.Services.CacheProvider;

			INode compNameNode = node.GetChild(0);

			if (compNameNode == null)
			{
				String message = String.Format("You must specify the component name on the #{0} directive", Name);
				throw new ViewComponentException(message);
			}

			string componentName = compNameNode.FirstToken.Image;

			if (componentName == null)
			{
				String message = String.Format("Could not obtain component name from the #{0} directive", Name);
				throw new ViewComponentException(message);
			}

			if (componentName.StartsWith("$"))
			{
				String nodeContent = compNameNode.Literal.Trim('"', '\'');
				SimpleNode inlineNode = runtimeServices.Parse(new StringReader(nodeContent), context.CurrentTemplateName, false);

				inlineNode.Init(context, runtimeServices);
				componentName = (string) Evaluate(inlineNode, context);
			}

			IDictionary componentParams = CreateParameters(context, node);

			Type viewComptype = registry.GetViewComponent(componentName);

			ViewComponentDescriptor descriptor = null;
			CacheKey key = null;

			if (viewComptype != null)
			{
				descriptor = viewDescProvider.Collect(viewComptype);
			}

			bool isOutputtingToCache = false;
			ViewComponentCacheBag bag = null;

			if (descriptor != null && descriptor.IsCacheable)
			{
				key = descriptor.CacheKeyGenerator.Create(componentName, componentParams, railsContext);

				if (key != null)
				{
					ViewComponentCacheBag cachedContent = (ViewComponentCacheBag) cacheProvider.Get(key.ToString());

					if (cachedContent != null)
					{
						// Restore entries

						foreach(KeyValuePair<string, object> pair in cachedContent.ContextEntries)
						{
							context[pair.Key] = pair.Value;
						}

						// Render from cache

						writer.Write(cachedContent.Content);

						return true;
					}

					isOutputtingToCache = true;
					bag = new ViewComponentCacheBag();
				}
			}

			ViewComponent component = viewComponentFactory.Create(componentName);

			if (component == null)
			{
				throw new MonoRailException("ViewComponentFactory returned a null ViewComponent for " + componentName + ". " +
				                            "Please investigate the implementation: " + viewComponentFactory.GetType().FullName);
			}

			try
			{
				ASTDirective directiveNode = (ASTDirective) node;
				IViewRenderer renderer = (IViewRenderer) directiveNode.Directive;

				NVelocityViewContextAdapter contextAdapter = new NVelocityViewContextAdapter(componentName, node, viewEngine, renderer);
				contextAdapter.Context = isOutputtingToCache ? new CacheAwareContext(context, bag) : context;

				INode bodyNode = null;

				if (node.ChildrenCount > 0)
				{
					bodyNode = node.GetChild(node.ChildrenCount - 1);
				}

				TextWriter output = isOutputtingToCache ? bag.CacheWriter : writer;

				contextAdapter.BodyNode = bodyNode;
				contextAdapter.ComponentParams = componentParams;
				contextAdapter.TextWriter = output;

				component.Init(railsContext, contextAdapter);

				ProcessSubSections(component, contextAdapter);

				const string ViewComponentContextKey = "viewcomponent";
				object previousComp = context[ViewComponentContextKey];

				try
				{
					context[ViewComponentContextKey] = component;

					component.Render();

					if (contextAdapter.ViewToRender != null)
					{
						RenderComponentView(context, contextAdapter.ViewToRender, output, contextAdapter);
					}

					if (isOutputtingToCache)
					{
						// Save output

						cacheProvider.Store(key.ToString(), bag);

						// Output to correct writer

						writer.Write(bag.Content);
					}
				}
				finally
				{
					if (previousComp != null)
					{
						context[ViewComponentContextKey] = previousComp;
					}
					else
					{
						context.Remove(ViewComponentContextKey);
					}
				}
			}
			finally
			{
				viewComponentFactory.Release(component);
			}

			return true;
		}
		protected virtual void ProcessSubSections(ViewComponent component, NVelocityViewContextAdapter contextAdapter)
		{
		}
		public bool RenderComponentView(IInternalContextAdapter context,
		                                String viewToRender, TextWriter writer,
		                                NVelocityViewContextAdapter contextAdapter)
		{
			if (!viewToRender.ToLower().EndsWith(NVelocityViewEngine.TemplateExtension))
			{
				viewToRender = viewToRender + NVelocityViewEngine.TemplateExtension;
			}

			CheckTemplateStack(context);

			String encoding = ExtractEncoding(context);

			Template template = GetTemplate(viewToRender, encoding);

			return RenderView(context, viewToRender, template, writer);
		}
		public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node)
		{
		    componentName = compNameNode.FirstToken.Image;

		    if (componentName == null)
		    {
		        String message = String.Format("Could not obtain component name from the #{0} directive", Name);
		        throw new ViewComponentException(message);
		    }

		    if (componentName.StartsWith("$"))
		    {
		        String nodeContent = compNameNode.Literal.Trim('"', '\'');
		        SimpleNode inlineNode = rsvc.Parse(new StringReader(nodeContent), context.CurrentTemplateName,
		                                                      false);

		        inlineNode.Init(context, rsvc);
		        componentName = (String) Evaluate(inlineNode, context);
		    }

            ViewComponent component = viewComponentFactory.Create(componentName);

		    
		        ASTDirective directiveNode = (ASTDirective) node;
		        IViewRenderer renderer = (IViewRenderer) directiveNode.Directive;
                NVelocityViewContextAdapter contextAdapter = new NVelocityViewContextAdapter(componentName, node, viewEngine, renderer);
		       contextAdapter.Context = context;



		        INode bodyNode = null;

		        IDictionary componentParams = CreateParameters(context, node);

		        if (node.ChildrenCount > 0)
		        {
		            bodyNode = node.GetChild(node.ChildrenCount - 1);
		        }

		        contextAdapter.BodyNode = bodyNode;
		        contextAdapter.ComponentParams = componentParams;
		        contextAdapter.TextWriter = writer;
                IRailsEngineContext railsContext = MonoRailHttpHandler.CurrentContext;

		        component.Init(railsContext, contextAdapter);

		        ProcessSubSections(component, contextAdapter);

		       

		        const string ViewComponentContextKey = "viewcomponent";
		        var previousComp = context[ViewComponentContextKey];

		        try
		        {
		            context[ViewComponentContextKey] = component;



		            component.Render();

		            if (contextAdapter.ViewToRender != null)
		            {
		                RenderComponentView(context, contextAdapter.ViewToRender, writer, contextAdapter);
		            }
		        }
		        finally
		        {
		            if (previousComp != null)
		            {
		                context[ViewComponentContextKey] = previousComp;
		            }
		            else
		            {
		                context.Remove(ViewComponentContextKey);
		            }
		        }
		    	

			return true;
		}
		public bool RenderComponentView(IInternalContextAdapter context, String viewToRender, TextWriter writer, NVelocityViewContextAdapter contextAdapter)
		{
			foreach(DictionaryEntry entry in contextAdapter.ContextVars)
			{
				context.Put(entry.Key.ToString(), entry.Value);
			}

			viewToRender = viewToRender + NVelocityViewEngine.TemplateExtension;

			CheckTemplateStack(context);

			String encoding = SetUpEncoding(context);

			Template template = GetTemplate(viewToRender, encoding);

			return RenderView(context, viewToRender, template, writer);
		}
Example #11
0
        public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node)
        {
            var railsContext     = EngineContextLocator.Instance.LocateCurrentContext();
            var registry         = railsContext.Services.GetService <IViewComponentFactory>().Registry;
            var viewDescProvider =
                railsContext.Services.GetService <IViewComponentDescriptorProvider>();
            var cacheProvider = railsContext.Services.CacheProvider;

            var compNameNode = node.GetChild(0);

            if (compNameNode == null)
            {
                var message = String.Format("You must specify the component name on the #{0} directive", Name);
                throw new ViewComponentException(message);
            }

            var componentName = compNameNode.FirstToken.Image;

            if (componentName == null)
            {
                var message = String.Format("Could not obtain component name from the #{0} directive", Name);
                throw new ViewComponentException(message);
            }

            if (componentName.StartsWith("$"))
            {
                var nodeContent = compNameNode.Literal.Trim('"', '\'');
                var inlineNode  = runtimeServices.Parse(new StringReader(nodeContent), context.CurrentTemplateName, false);

                inlineNode.Init(context, runtimeServices);
                componentName = (string)Evaluate(inlineNode, context);
            }

            var componentParams = CreateParameters(context, node);

            var viewComptype = registry.GetViewComponent(componentName);

            ViewComponentDescriptor descriptor = null;
            CacheKey key = null;

            if (viewComptype != null)
            {
                descriptor = viewDescProvider.Collect(viewComptype);
            }

            var isOutputtingToCache   = false;
            ViewComponentCacheBag bag = null;

            if (descriptor != null && descriptor.IsCacheable)
            {
                key = descriptor.CacheKeyGenerator.Create(componentName, componentParams, railsContext);

                if (key != null)
                {
                    var cachedContent = (ViewComponentCacheBag)cacheProvider.Get(key.ToString());

                    if (cachedContent != null)
                    {
                        // Restore entries

                        foreach (var pair in cachedContent.ContextEntries)
                        {
                            context[pair.Key] = pair.Value;
                        }

                        // Render from cache

                        writer.Write(cachedContent.Content);

                        return(true);
                    }

                    isOutputtingToCache = true;
                    bag = new ViewComponentCacheBag();
                }
            }

            var component = viewComponentFactory.Create(componentName);

            if (component == null)
            {
                throw new MonoRailException("ViewComponentFactory returned a null ViewComponent for " + componentName + ". " +
                                            "Please investigate the implementation: " + viewComponentFactory.GetType().FullName);
            }

            try
            {
                var directiveNode = (ASTDirective)node;
                var renderer      = (IViewRenderer)directiveNode.Directive;

                var contextAdapter = new NVelocityViewContextAdapter(componentName, node, viewEngine, renderer)
                {
                    Context = isOutputtingToCache ? new CacheAwareContext(context, bag) : context
                };

                INode bodyNode = null;

                if (node.ChildrenCount > 0)
                {
                    bodyNode = node.GetChild(node.ChildrenCount - 1);
                }

                var output = isOutputtingToCache ? bag.CacheWriter : writer;

                contextAdapter.BodyNode        = bodyNode;
                contextAdapter.ComponentParams = componentParams;
                contextAdapter.TextWriter      = output;

                component.Init(railsContext, contextAdapter);

                ProcessSubSections(component, contextAdapter);

                const string ViewComponentContextKey = "viewcomponent";
                var          previousComp            = context[ViewComponentContextKey];

                try
                {
                    context[ViewComponentContextKey] = component;

                    component.Render();

                    if (contextAdapter.ViewToRender != null)
                    {
                        RenderComponentView(context, contextAdapter.ViewToRender, output, contextAdapter);
                    }

                    if (isOutputtingToCache)
                    {
                        // Save output

                        cacheProvider.Store(key.ToString(), bag);

                        // Output to correct writer

                        writer.Write(bag.Content);
                    }
                }
                finally
                {
                    if (previousComp != null)
                    {
                        context[ViewComponentContextKey] = previousComp;
                    }
                    else
                    {
                        context.Remove(ViewComponentContextKey);
                    }
                }
            }
            finally
            {
                viewComponentFactory.Release(component);
            }

            return(true);
        }
Example #12
0
 protected virtual void ProcessSubSections(ViewComponent component, NVelocityViewContextAdapter contextAdapter)
 {
 }