In the first part, we wrote simple extension that changes case in the post to lower. Let’s say, we want user to decide show post in the lower or upper case. For this, we need to be able to maintain variable and let blogger change it’s value through admin interface. Normally, you would need to add a data access functionality for extension to handle this kind of operation, create admin form etc. BlogEngine uses Extension Manager to help make job a lot easier. Take a look at the code below. In the constructor, we subscribe to post serving event and then call InitSettings. There we create ExtensionSettings object, which can hold singe value (scalar) or/and table of values. In our case, we want it to hold single scalar value, so we set settings to scalar. We call our variable “Case” and initialize it with default value of “upper”, then call ExtensionManager.InitSettings and pass name of extension and settings object.

using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
 
[Extension("Example_02", "1.0", "<a href=\"http://me.net\">Me</a>")]
public class Example_02
{
    static protected ExtensionSettings _settings = null;
 
    public Example_02()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(ServingHandler);
        InitSettings();
    }
 
    private void ServingHandler(object sender, ServingEventArgs e)
    {
        if (_settings.GetSingleValue("Case") == "upper")
            e.Body = e.Body.ToUpper();
        else
            e.Body = e.Body.ToLower();
    }
 
    private void InitSettings()
    {
        ExtensionSettings settings = new ExtensionSettings(this);
        settings.IsScalar = true;
        settings.AddParameter("Case");
        settings.AddValue("Case", "upper");
        _settings = ExtensionManager.InitSettings("Example_02", settings);
    }
}

If you go to the settings list, you’ll see a link to the settings object next to the extension name. Clicking it will bring the screen shown below. Here blogger can maintain variable that we can use in the extension to determine what case to apply to the post. And this is what ServingHandler function does: it uses settings object saved during instantiation to get value of the “Case” parameter and, if it has value “upper” – set post to upper, else – set to lower. When value changed, it will be saved either in the file system or in the database – depending on the provider that blog uses.

tut2-2

The case variable was a string, but there are other data typed we could use. The Example_03 shows which types are supported by BlogEngine 1.4. 

using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Collections.Generic;
using System.Collections.Specialized;
 
[Extension("Example_03", "1.0", "<a href=\"http://me.net\">Me</a>")]
public class Example_03
{
  static protected ExtensionSettings _settings = null;
 
  public Example_03()
  {
    Comment.Serving += new EventHandler<ServingEventArgs>(Post_CommentServing);
    InitSettings();
  }
 
  private void Post_CommentServing(object sender, ServingEventArgs e)
  {
    Comment comment = (Comment)sender;
    if (comment.Author != "typetest")
      return;
 
    foreach (ExtensionParameter p in _settings.Parameters)
    {
      if (string.IsNullOrEmpty(p.SelectedValue))
        e.Body += "<br/>" + p.Name + ": " + p.Values[0];
      else
        e.Body += "<br/>" + p.Name + ": " + p.SelectedValue;
    }
  }
 
  private void InitSettings()
  {
    ExtensionSettings settings = new ExtensionSettings(this);
    settings.IsScalar = true;
 
    // define parameters
    settings.AddParameter("TheString");
    settings.AddParameter("TheBoolean", "The boolean");
    settings.AddParameter("TheInteger", "The integer", 10);
    settings.AddParameter("TheLong", "The Long", 20, false);
    settings.AddParameter("TheFloat", "The Float", 10, false, false, ParameterType.Float);
    settings.AddParameter("TheDouble", "The Double", 15, false, false, ParameterType.Double);
    settings.AddParameter("TheDecimal", "The Decimal", 20, false, false, ParameterType.Decimal);
    // lists
    settings.AddParameter("TheDropdown");
    settings.AddParameter("TheListBox", "The ListBox", 20, false, false, ParameterType.ListBox);
    settings.AddParameter("TheRadioGroup", "The RadioGroup", 20, false, false, ParameterType.RadioGroup);
 
    // set default values
    settings.AddValue("TheString", "Test string");
    settings.AddValue("TheBoolean", true);
    settings.AddValue("TheInteger", 25);
    settings.AddValue("TheLong", 9223372036854);
    settings.AddValue("TheFloat", 25.7);
    settings.AddValue("TheDouble", 523456789.35);
    settings.AddValue("TheDecimal", decimal.Parse("9223372036854342342.345"));
    // lists
    StringCollection dd = new StringCollection();
    dd.AddRange(new string[] { "One", "Two", "Three" });
    settings.AddValue("TheDropdown", dd, "Three");
    settings.AddValue("TheListBox", new string[] { "List1", "List2", "List3" }, "List2");
    settings.AddValue("TheRadioGroup", new string[] { "Radio1", "Radio2", "Radio3" }, "Radio1");
 
    _settings = ExtensionManager.InitSettings(this.GetType().Name, settings);
  }
}

tut2-3

As you can see from the example and the picture, there are standard types like integer, decimal etc. Boolean, as one would expect, rendered as check box. There are also List data types. Lists render on the admin form in three different ways: as a drop down, as list box and as radio button group. There is standard type checking – if you try to save not integer value in the parameter declared as integer, you’ll get an error message. Check Exemple_03 code on how to declare different types of parameters. Just note that if you not specify parameter type and pass to AddParameter, say, integer – BE will check parameter type and make a best guess, assigning integer, decimal etc. based on variable you passed as default value.

This example uses comment serving event, which is fired when comment is served. For the purpose of showing how to get values back from settings, event handler outputs each parameter and its value in the comment body – if commenter is “typetest”. In the standard theme, you can switch to preview to see what it looks like.

tut2-1

 

To get strongly typed value, use conversion function. For example:

int i = int.Parse(p.Values[0]);

Next time I’ll go over Extension Manager and ways of using it in BlogEngine 1.4.

Share/Save/Bookmark
Signature

Comments

7/10/2008 2:14:45 AM #

Muhammad Mosa

Excellent walkthrough, looking forward next part!
Cheers!

Muhammad Mosa |

7/17/2008 4:30:13 AM #

Cristiano

Hi Ruslan,
I have found a strange bug after upgrading to BE 1.4. The list of extensions sometimes shows several times a copy of the same item (and the loading time of the page is increased).
Some suggestion ?

Cristiano |

7/17/2008 5:21:16 PM #

rtur.net

I've seen something similar on Mono/Linux, but never in 1.4 on Windows (and I'm running it for quite a while, including this site). Can you give me some details on your environment and may be you noticed any pattern with regards when you getting these duplicates?

rtur.net |

7/18/2008 12:23:40 AM #

Cristiano

Well, i'm running BE 1.4.0.9 with these extensions:

- AdsenseInjector (Enabled)
- BBCode (Enabled)
- BBCodeToolbar (Disabled)  
- BreakPost (Enabled)
- CodeFormatterExtension (Enabled)  
- CommentToolbar (Enabled)
- DownloadCounter (Disabled)
- FeedFlare (Disabled)
- HotlinkingImgBlock (Enabled)
- MetaAddin (Disabled)
- ResolveLinks (Enabled)  
- SendCommentMail (Enabled)
- SendPings 1.3 (Enabled)
- SimpleDownloadCounter (Enabled)
- Smilies 1.2.1 (Enabled)
- TopPosts (Enabled)
- Wikipedia (Enabled)
- YouTube (Enabled)

After the upgrade from BE 1.3, I copied from the file extensions.xml the single configuration extensions in each extension folders.

Cristiano |

7/29/2008 7:36:13 PM #

Brian Schreder

Is there some way to determine or set the order in which extensions are applied?  For example, I have a couple of extensions that append html strings to the bottom of my single posts (e.Body += <string>).   I would like to set the order the appending of these strings.

Brian Schreder |

7/31/2008 11:24:52 AM #

rtur.net

No, BE does not pre-order extensions, it just walks through assembly and loads any class marked with "extension" attribute. I ran into this issue myself, probably needs to be addressed. You can do it yourself in global.asax - add string collection, sort it the way you like and then load extension in that order.

rtur.net |

1/11/2009 8:19:49 AM #

rtur.net

Is there some way to determine or set the order in which extensions are applied?
There is now:
http://rtur.net/blog/post/2009/01/11/Setting-priorities-for-extensions.aspx

rtur.net |

1/15/2009 10:21:47 PM #

gamt

i see it but i cant i think thatsamting is wrong with the url

gamt |

Comments are closed
<<  July 2009  >>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Protected by Commentor
256 comments approved
370 spam caught
Since December 1, 2008
Powered by Spam Counter
Enhanced with Snapshots

Subscribe to Rtur.net