Separating design from data, iterating over external data
I'm considering Umbraco as a base for an application my company is building and I'm trying to determine the right way to do certain things. I've gotten a license to Umbraco.tv and watched a bunch of videos as well as read lots of content. I'm still not certain how to display external data so I'm turning to the forum.
Basically, I want to be able to get external data from a web service and display it on a page. Seems simple enough and I could certainly do that with a .net usercontrol macro but then I'd be tying my display and data together (since the usercontrol would get the data and then display that data and ideally I'd like my designers to be able to decide how to display the data). The only other way I can see to extend Umbraco in this way is to use an XSLT macro. Aside from the intricacies of XSLT and my concern over how the macro would actually get the data from the webservice, this probably wouldn't work without some massive hack because the data the webservice returns is JSON, not XML.
Is there a built in way to handle this sort of separation of concerns? Ideally I'd like to be able to have some code return an IEnumerable<Widget> and then have a template, which, being HTML could be easily edited by a designer, have a foreach (using a razor style psuedocode):
<ul> @foreach(var w in widgets) { <li>@w.Property1</li> } </ul>
and then the next designer could change the <ul>/<li> to a bunch of divs if they wanted to.
What's the 'right' way to accomplish this in Umbraco?
If the web service returns JSON, have you considered using JQuery or similar to render out the widgets? If you need the widgets to be visible to search engines though, this isn't really an option (as the majority of search engines don't run JS when they index).
Failing that, you could create a data access class that talks to the web service, gets the results and parses it into IEnumerable<Widget> and returns that. You can then just call the class in your Razor macro using something like:
var widgetList = MyDataClass.GetWidgetList();
@foreach(var w in widgets){
}
If you were using user controls rather than Razor, you could also accomplish the same thing with a repeater bound to the output of the data access class.
Separating design from data, iterating over external data
I'm considering Umbraco as a base for an application my company is building and I'm trying to determine the right way to do certain things. I've gotten a license to Umbraco.tv and watched a bunch of videos as well as read lots of content. I'm still not certain how to display external data so I'm turning to the forum.
Basically, I want to be able to get external data from a web service and display it on a page. Seems simple enough and I could certainly do that with a .net usercontrol macro but then I'd be tying my display and data together (since the usercontrol would get the data and then display that data and ideally I'd like my designers to be able to decide how to display the data). The only other way I can see to extend Umbraco in this way is to use an XSLT macro. Aside from the intricacies of XSLT and my concern over how the macro would actually get the data from the webservice, this probably wouldn't work without some massive hack because the data the webservice returns is JSON, not XML.
Is there a built in way to handle this sort of separation of concerns? Ideally I'd like to be able to have some code return an IEnumerable<Widget> and then have a template, which, being HTML could be easily edited by a designer, have a foreach (using a razor style psuedocode):
<ul>
@foreach(var w in widgets) {
<li>@w.Property1</li>
}
</ul>
and then the next designer could change the <ul>/<li> to a bunch of divs if they wanted to.
What's the 'right' way to accomplish this in Umbraco?
Hi Noah,
If the web service returns JSON, have you considered using JQuery or similar to render out the widgets? If you need the widgets to be visible to search engines though, this isn't really an option (as the majority of search engines don't run JS when they index).
Failing that, you could create a data access class that talks to the web service, gets the results and parses it into IEnumerable<Widget> and returns that. You can then just call the class in your Razor macro using something like:
var widgetList = MyDataClass.GetWidgetList();
@foreach(var w in widgets){
}
If you were using user controls rather than Razor, you could also accomplish the same thing with a repeater bound to the output of the data access class.
is working on a reply...