What is wrong with this Widget Extension?

I have created a widget extension to return a simple array, but it won't work. Here it is:

namespace LocationData
{
    public class Location
    {

        public Array GetAllCountries()
        {
            string[] countries = { "United States", "United Kingdom", "Canada"};
            return countries;
        }

    }

    public class LocationWidgetExtension : IScriptedContentFragmentExtension
    {
        #region IScriptedContentFragmentExtension Members

        public string ExtensionName
        {
            get { return "Ibby_v1_LocationData"; }
        }

        public object Extension
        {
            get { return new Location(); }
        }

        #endregion

        #region IPlugin Members

        public string Name
        {
            get { return "GetLocationData"; }
        }

        public string Description
        {
            get { return "Get Location Data"; }
        }

        public void Initialize()
        {
        }

        #endregion
    }

}

And the Velocity Script

#set ($countries = $ibby_v1_LocationData.GetAllCountries())
#foreach($country in $countries)
    <span>$country</span>
#end

Parents
  • Have you enabled the extension on your site?  You also need to match the capitalization you defined in your extension.  It is defined as Ibby_v1_LocationData, but you are attempting to use it as ibby_v1_LocationData.

  • That worked, thanks. Also, can I return an array of objects? will that work?

  • I don't think there would be any reason an Array could not be returned.

  • I have modified the Widget Extension to return an array of objects, but they are not getting output as I want. Here's the code:

    namespace LocationData
    {
        public class Location
        {
    
            public Array GetAllCountries()
            {
                SqlDataAccess sd = new SqlDataAccess();
                SqlCommand sc = sd.GetCommand("EXEC ibby_GetAllCountries");
                DataTable dt = sd.Execute(sc);
                Item ct = new Item();
                List<Item> li = new List<Item>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ct = new Item();
                    ct.iso2 = dt.Rows[i]["iso2"].ToString();
                    ct.country = dt.Rows[i]["country"].ToString();
                    li.Add(ct);
                }
                return li.ToArray(); //dt.AsEnumerable().Select(r => r.Field<string>("country")).ToArray();
            }
    
        }
    
        public class LocationWidgetExtension : IScriptedContentFragmentExtension
        {
            #region IScriptedContentFragmentExtension Members
    
            public string ExtensionName
            {
                get { return "Ibby_v1_LocationData"; }
            }
    
            public object Extension
            {
                get { return new Location(); }
            }
    
            #endregion
    
            #region IPlugin Members
    
            public string Name
            {
                get { return "GetLocationData"; }
            }
    
            public string Description
            {
                get { return "Get Location Data"; }
            }
    
            public void Initialize()
            {
            }
    
            #endregion
        }
    
        public class Item
        {
            public string iso2;
            public string country;
        }
    
    }

    And here's the Velocity

    #set ($countries = $Ibby_v1_LocationData.GetAllCountries())
    #foreach($item in $countries)
        <span>$item.iso2</span>|<span>$item.country</span><br/>
    #end

    And this is what is being output:

    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country
    $item.iso2|$item.country

  • I would recommend returning a List<Item> rather than an Array.  Since the array is not typed, velocity does not know what the Objects inside it are.

Reply Children