Base class for reusable UI Components.
Implementors should override ViewComponent.Initialize for implement proper initialization (if necessary). Also implement ViewComponent.Render as by default it will render a default view on [ViewFolderRoot]/components/[componentname].

You can also override ViewComponent.SupportsSection if your component supports neste sections (ie templates provided on the view that uses the view component.

Another way is to use the ViewComponentDetailsAttribute to specify a custom name and the sections supported.

Notice that view components can render strings or views on their folder. You can create sophisticate components with that mixture. Sections allow the users of your component to give it a block of content, usually customizing or definiting the content to be especially rendered by your component.

A very simplist view component that renders the time. public class ShowTime : ViewComponent { public override void Initialize() { } public override void Render() { RenderText("Time: " + DateTime.Now.ToString()); } }

This can be used from the view using the following syntax (NVelocity view engine)

#component(ShowTime)
		internal void CreateViewComponent()
		{
			if ((viewComponentName == null) || (viewComponentName.Length < 1))
			{
				throw new RailsException("You must specify the component name with 'blockcomponent' and 'component'.");
			}
			viewComponent = ((ViewComponentStringTemplateGroup)group).ViewComponentFactory.Create(viewComponentName);
			viewComponentContext = new StringTemplateViewContextAdapter(viewComponentName, this);
		}
		public override void Release(ViewComponent instance)
		{
			if (kernel.HasComponent(instance.GetType()))
			{
				kernel.ReleaseComponent(instance);
			}
			else
			{
				base.Release(instance);
			}
		}
		/// <summary>
		/// Initialize the view component with mock services it needs to 
		/// be functional.
		/// </summary>
		/// <param name="component">The component instance.</param>
		protected void PrepareViewComponent(ViewComponent component)
		{
			if (Context == null)
			{
				BuildEngineContext("", "Controller", "Action");
			}

			viewEngine = BuildViewEngine();

			componentContext = BuildViewComponentContext(component.GetType().Name);

			component.Init(Context, componentContext);
		}
		protected virtual void ProcessSubSections(ViewComponent component, NVelocityViewContextAdapter contextAdapter)
		{
		}
Esempio n. 5
0
 public ViewComponentInfo(ViewComponent component)
 {
     Type = component.GetType();
     Details = Type.GetCustomAttributes(typeof(ViewComponentDetailsAttribute), false).OfType<ViewComponentDetailsAttribute>().FirstOrDefault();
     Instance = component;
 }