How to use a different view for a Widget based on certain conditions

Topics: Writing modules
May 17, 2013 at 3:23 AM
This may be a simple question, but I am a bit confused as to how to approach this.
  • I have a content part named "ActiveRecordSelections" that keeps track of certain active information such as
    • Active School
    • Active School Year
    • Active Student
When a parent logs in, the widget shows up in aside first and they can select which teacher, school, school year and student they want to use.

When a "teacher" logs in, the widget should render in "Navigation" and only allow them to switch between schools.

I thought it would be possible to somehow just have 2 views and then in my driver do something like
if(isTeacher)
{
  DriverResult shape = ContentShape("Parts_ActiveRecordSelectionsForTeachers",
            () => shapeHelper.Parts_ActiveRecordSelectionsForTeachers());
}
else //the default
{
  DriverResult shape = ContentShape("Parts_ActiveRecordSelections",
            () => shapeHelper.Parts_ActiveRecordSelections);
}
This doesn't work as I don't really understand how it's all supposed to work. It seems like I should be able to direct to one view or another from the driver after evaluating certain logic...

Is this possible?
Developer
May 17, 2013 at 11:46 PM
So you have a single widget (with the ActiveRecordSelections part attached), and want to render this widget in one zone or the other, depending on the type of user (teacher or parent),

A Widget is always added to a single zone. However, you can use Placement to configure where all the shapes are to be added to (local zones or global zones).
So one way is to create a Match element that matches your widget content type, and Place element(s) with shape attributes to designate a zone. Using the new / syntax, you can dispatch the Parts_ActiveRecordSelectionsForTeachers shape to one zone, and the Parts_ActiveRecordSelections to another. A widget emits more sdhapes, since typically a widget contains more parts, whose drivers emit shapes. You can hide those shapes by configuring them to not render (using Placement.info).

Another way could be to add two widgets: one widget in AsideFirst and another in Navigation. Then use a layer rule to show either one or the other. Your driver's logic would remain as is, because you still want to render one shape or the other.