Innehållstyper

Innehållstyper är tillsammans med text de grundläggande byggstenarna i de sidor som redaktörerna arbetar med. Dynamix tillåter att innehållstyper kombineras helt fritt och det är också enkelt att definiera egna utökningar.

Klasser och definitioner

En innehållstyp består av tre delar; definition, information och presentation. Dessa tre delar implementeras som tre klasser som ska ärvas från:

Innehållstypens tre delar

Dynamix.ContentType - definition

Detta är definitionen av innehållstypen mot redaktören och systemet. Varje innehållstyp har en unik instans av ett sådant objekt i databasen (unikt UID krävs, sätts i konstruktorn). Här definieras egenskaper som visas i gränssnittet (namn, visningsikon mm). Objektet är ansvarigt för att skapa klassen som används för att lagra information om enskilda innehåll i sidor, samt för att skapa kontrollen som renderas i sidan.

Exempel:

  1. C#
  1. using System;
  2. using System.Web.UI;
  3. using Dynamix;
  4. using Dynamix.Content;
  5. using System.Runtime.Serialization;
  6. namespace MySite.ContentTypes
  7. {
  8. [Serializable]
  9. [Dynamix.Definitions.ContentType]
  10. public class NewsListContentType : Dynamix.ContentType
  11. {
  12. // Serialization and database constructors - required by Dynamix
  13. protected NewsListContentType ( ObjectID id ) : base( id ) {}
  14. protected NewsListContentType (
  15. SerializationInfo info, StreamingContext context ) :
  16. base( info, context ) {}
  17. public NewsListContentType () : base() {}
  18. public override string IconUrl
  19. {
  20. get { return Dynamix.Util.Icon16( "news" ); }
  21. }
  22. public override string Name
  23. {
  24. get { return "Nyhetslista"; }
  25. }
  26. // Returns the object that contains Information
  27. public override ContentComponent CreateContentComponent()
  28. {
  29. return new NewsListComponent();
  30. }
  31. // Returns the control for Presentation
  32. public override ContentControl CreateContentControl( ContentComponent cc )
  33. {
  34. return new NewsListControl();
  35. }
  36. // Instance method for easy access from code
  37. public static NewsListContentType Instance
  38. {
  39. get
  40. {
  41. return (NewsListContentType)GetInstance( typeof( NewsListContentType ) );
  42. }
  43. }
  44. }
  45. }

notera attributet [Dynamix.Definitions.ContentType] som gör att Dynamix kan upptäcka och installera innehållstyper. Läs mer om definitionsattribut här.

ContentComponent - Information

Objekt av denna klass lagrar information om de innehåll som ligger i själva sidorna. Normalt lagras sådana egenskaper som redaktörerna kan påverka. Det kan vara till exempel kopplingar till bilder, textfält, länkar eller kategoriurval. Utvecklaren lägger fritt till egenskaper.

Exempel:

  1. C#
  1. using System;
  2. using System.Web.UI;
  3. using Dynamix;
  4. using System.Runtime.Serialization;
  5. namespace MySite.ContentTypes
  6. {
  7. [Serializable]
  8. [Descriptors.UI.EditObjectDialogDescriptor( typeof( Dynamix.UI.Dialogs.GeneralEditContentComponent.UIEditObjectDialogDescriptor ) )]
  9. public class NewsListComponent : Dynamix.ContentComponent
  10. {
  11. public static readonly ObjectListProperty p_Categories =
  12. new ObjectListProperty( "Categories", typeof( Dynamix.Category ) );
  13. public static readonly IntProperty p_MaxCount =
  14. new IntProperty( "MaxCount", -1 );
  15. // Serialization and database constructors - Required by Dynamix
  16. protected NewsListComponent( ObjectID id ) : base( id ) {}
  17. protected NewsListComponent(
  18. SerializationInfo info, StreamingContext context ) :
  19. base( info, context ) {}
  20. public NewsListComponent() : base() {}
  21. public ObjectList Categories
  22. {
  23. get { return GetPropertyList( p_Categories ); }
  24. }
  25. public int MaxCount
  26. {
  27. get { return GetPropertyValue( p_MaxCount ); }
  28. set { SetPropertyValue( p_MaxCount, value ); }
  29. }
  30. public ObjectList GetItems()
  31. {
  32. ...
  33. }
  34. }
  35. }

ContentControl - Presentation

Denna klass ska ärvas från Dynamix.Content.ContentControl (som ärver från WebControl). Det går alltså att överlagra CreateChildControls, Render m fl. Tillgång till informationsobjektet ges via egenskapen ContentComponent och till sidan via egenskapen DynamixContentPage.

Exempel:

  1. C#
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Dynamix;
  5. using System.Runtime.Serialization;
  6. namespace MySite.ContentTypes
  7. {
  8. public class NewsListControl : Dynamix.Content.ContentControl
  9. {
  10. public NewsListControl()
  11. {
  12. }
  13. protected override void CreateChildControls()
  14. {
  15. NewsListComponent info = (NewsListComponent)ContentComponent;
  16. ObjectList items = info.GetItems();
  17. if( items.Count == 0 && InEditMode )
  18. {
  19. Controls.Add( new LiteralControl( "[Nyhetslista]" ) );
  20. }
  21. else
  22. {
  23. Table t = new Table();
  24. foreach( NewsItem item in items )
  25. {
  26. TableRow tr = new TableRow();
  27. TableCell tdDate = new TableCell();
  28. tdDate.Text = item.Date;
  29. TableCell tdHeadline = new TableCell();
  30. tdHeadline.Text = item.Headline;
  31. tr.Cells.Add( tdDate );
  32. tr.Cells.Add( tdHeadline );
  33. t.Rows.Add( tr );
  34. }
  35. Controls.Add( t );
  36. }
  37. }
  38. }
  39. }

Att skapa egna innehållstyper

Vanligtvis skapas de tre ovanstående klasserna i samma fil. Innehållstypen kan sedan installeras från verktyget "Innehållstyper" eller via ContentType.DB_Install()