7 Creating more reusable components

 

This chapter covers

  • Using templates to define specific regions of UI
  • Enhancing templates with generics
  • Sharing components using Razor class libraries

Reusability is one of the compelling reasons for using components. They allow us to define chunks of markup and logic that can be reused by simply referencing them in other markup. This is an immensely powerful tool. So far, we’ve taken advantage of this on several occasions in previous chapters:

  • TrailCard component
  • FormSection component
  • FormFieldSet component
  • SuccessAlert and ErrorAlert components
NOTE

If you’re following along building the example application, you will need to complete appendix B before starting this chapter.

In this chapter, we’re going to take reusability to the next level. We’ll learn how to leverage templates and generics to make the ultimate reusable components. To give us a practical example, we’ll be enhancing the home page of Blazing Trails with a component that allows the user to toggle the layout between a grid and a table (figure 7.1).

Figure 7.1 The home page of Blazing Trails with the final ViewSwitcher component we’ll be building during this chapter. The component allows the user to toggle between a grid view and a table view of the available trails.
CH07_F01_Sainty

7.1 Defining templates

7.2 Enhancing templates with generics

7.3 Sharing components with Razor class libraries

Summary

sitemap