daveaglick / FluentBootstrap

Provides extensions, helper classes, model binding, and other goodies to help you use the Bootstrap CSS framework from .NET code.
http://www.fluentbootstrap.com
MIT License
200 stars 76 forks source link

Writing custom extension methods for nested elements #39

Closed jptillman closed 9 years ago

jptillman commented 9 years ago

I'm trying to write some custom extension methods and have been successful with ones like this:

    public static ComponentBuilder<MvcBootstrapConfig<TModel>, Element> TaskDialogBody<TComponent, TModel>(
        this BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, string title)
        where TComponent : Component, ICanCreate<Tag>
    {
        return helper.Div(helper.Heading2(title).ToString()).AddCss("dialog-header");
    }

But I can't get my head around how to create nested elements without that ToString() call. For example, I need an extension method a div within a div and allow the caller to provide the content to be inserted in the inner div. How do I achieve this without converting the interior div to a string first? Any examples someone can point me to?

daveaglick commented 9 years ago

At first glance, I think you want the .AddChild(...) extension (take a look at http://www.fluentbootstrap.com/GettingStarted near the bottom under "Children"). For example:

 public static ComponentBuilder<MvcBootstrapConfig<TModel>, Element> TaskDialogBody<TComponent, TModel>(
        this BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, string title)
        where TComponent : Component, ICanCreate<Tag>
    {
        return helper.Div().AddCss("dialog-header").AddChild(helper.Heading2(title));
    }

Let me know if that doesn't work for you and I'll dig a little deeper. BTW, impressed you're diving into writing your own extensions! That was always a design goal, but the crazy amount of generics I had to use to make the calling API easier acts as a pretty big barrier. I keep meaning to write some good documentation about it, but you know how that goes...

jptillman commented 9 years ago

Thanks. If I gain some traction with this, I'll try to offer up some docs. I was within a hair's breadth of writing my own fluent bootstrap helper and, lo and behold, your library came up in a Google search and it works great. I'd love to "give back" somehow.

daveaglick commented 9 years ago

Awesome, looking forward to learning how you progress in your efforts. Even if you don't have time to write docs, etc. the fact that others are getting use of the library is satisfying in and of itself. I'll leave this issue open for a while in case you have any additional problems or questions.