0

I want to do something like the following in my spark view.

@{this.LinkTo("1234")}

Which should output something like

<a href="domain.tld?var=1234">1234</a>

I cannot seem to find a good way to do this.

Most searches for "fubumvc htmlhelpers" end up giving me more pages about htmlhelpers in msmvc.

A plus would be if I could put the code in a separate assembly that I can share between multiple sites.

Solution

namespace MyNamespace
{
    public static class FubuPageExtensions
    {
        public static HtmlTag LinkTo(this IFubuPage page, string input)
        {
            return new LinkTag(input, "domain.tld?var={0}".ToFormat(input));
        }
    }
}

...and in my spark view

<use namespace="MyNamespace" />

${this.LinkTo(Model.Something)}
Pingvinen
  • 65
  • 1
  • 5

3 Answers3

0

I had a similar requirement and I solved it this way (not sure if this is the best approach, but it worked for my scenario).

The idea is to create an extension method on the IFubuPage interface, which returns a new HtmlTag object. Please note that I'm using the Razor view engine, not entirely sure if this would work for Spark as well.

So for example, the following code would produce a new <abbr /> tag:

public static HtmlTag TimeAgoFor(this IFubuPage page, DateTime input)
{
    return new HtmlTag("abbr")
        .Title(input.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssK"))
        .AddClass("timeago")
        .Text(input.ToString("dd-MM-yyyy HH:mm"));
    }
}

In your scenario, I think this should suffice then:

public static HtmlTag LinkTo(this IFubuPage page, string input)
{
     return new LinkTag(input, "domain.tld?var={0}".ToFormat(input));
}
Dennis
  • 714
  • 5
  • 12
  • I have actually already tried this, but I just get a "Dynamic view compilation failed" stating that my method does not exist. I have not done anything other that creating the extension method within my own namespace. I do not know if Fubu would "magically" pick up my extension method, but it does not seem to do so. – Pingvinen May 02 '12 at 14:08
  • In the generated view / error screen do you see your namespace getting imported? If not you'll need to import it. – Corey Kaylor May 02 '12 at 14:22
  • My namespace is not referenced, but how do I get it imported? The is not currently accounted for. http://readthedocs.org/docs/fubumvc/en/latest/topics/viewengines/spark/usingspark.html#fubumvc-spark-limitations – Pingvinen May 02 '12 at 14:29
  • Apparently the documentation is out of date. I just tried the and it works. Thanks for all your help :) – Pingvinen May 02 '12 at 14:36
  • I'm not sure if that's accurate or up to date. We typically add our references in the _global.spark and it works fine. I haven't tried in the specific views template, but I would give it a try in both. If it does work let us know and we'll update the docs to reflect that. – Corey Kaylor May 02 '12 at 14:37
  • FWIW, the is accounted for in any references in your Spark View. It is NOT accounted for in your declaration. That's what this is "supposed" to mean: "The full type name must be specified in the Spark file. The is not currently accounted for." – jmarnold May 02 '12 at 14:42
0

Make sure you have your "FubuPageExtensions" namespace in your spark file.

As noted in the comments, use a "_global.spark" file in the Shared directory so all .spark files have the extensions namespace.

Joey V.
  • 1,866
  • 1
  • 22
  • 18
0

I don't know if this would appeal to you but you can also declare your helpers in one of the default namespaces that the fubu.spark viewengine supports, e.g.:

namespace FubuMVC.Core.UI
{
public static class MyHelper{
public static HtmlTag LinkTo(this IFubuPage page, string input)
{
     return new LinkTag(input, "domain.tld?var={0}".ToFormat(input));
}
}
}
Jaime Febres
  • 1,267
  • 1
  • 10
  • 15