Writing first token help

Topics: Writing modules
Feb 26, 2014 at 1:39 AM
Have written a token using the comments module as a guide. I think I got it right but I can't find a way to evaluate it (long story) and tracing this doesn't seem to agree with the debugger so can someone tell me if they see any problems with code?

The token is for a "Post" content-item in the Forum module . PostPart has a property called "Text".
public class PostTokens : ITokenProvider {
    public PostTokens() {
        T = NullLocalizer.Instance;
    }
    public Localizer T { get; set; }

    public void Describe(DescribeContext context) {
        context.For("ForumPost", T("Posts"), T("Posts made to the forums."))
            .Token("PostMessage", T("Post Message"), T("The text of the post itself"))          
            ;
    }

    public void Evaluate(EvaluateContext context) {
        context.For<IContent>("ForumPost")
            .Token("PostMessage", 
                content => content.As<PostPart>().Text)
            .Chain("PostMessage", "Text", 
                content => content.As<PostPart>().Text)             
            ;
    }

}

Would also like to ask for clarification on two variable (which might be one of my problems in the first place):

1)The "ForumPost" parameter in the above can be any arbitrary name? Or does it have some convention it needs to follow?
2) The "IContent" in context.For<IContent> is used for casting purposes?
Feb 26, 2014 at 7:37 PM
Okay I redid some of the code and figured out how to trace into where I need to be ....
       public void Describe(DescribeContext context) {
            context.For("Post", T("Post"), T("A post made to a forum."))
                .Token("PostMessage", T("Post Message"), T("The text of the post itself"))          
                ;
        }

        public void Evaluate(EvaluateContext context) {
            context.For<IContent>("Post")
                .Token("PostMessage", 
                    (GetText))
                ;
        }

        public string GetText(IContent content)
        {
            var x = content.As<PostPart>().Text;
            return x;
        }
When the evaluation runs, it looks like it has the correct information however the "GetText" never runs.

Image

(The 'Data' there is the Post content-item which has the PostPart and the Text property is set.)

I am missing some basic understanding of how to setup tokens for use. Why that doesn't evalutate. Any pointer?
Developer
Mar 30, 2014 at 10:30 PM
I think you need something like...

context.For<IContent>("Content")
 .Token("PostMessage", x=>x.As<PostPart>().Text)
Mar 31, 2014 at 7:59 AM
Thanks I'll try that. Working on something else at the moment but I'll post back when I get a chance to try it. In the bigger picture I am trying to get the token working so I can finish tying the forums into the antispam. I think they'll need a very small change. I'll post that back as well in the appropriate place when I get going on this again.
Jul 5, 2014 at 10:20 AM
Ok yes that works. It has to be "Content" and cannot be "Post" as I used in the example. As well in the Describe function the context.For must be "Content" as well i.e. context.For("Content", T("Post")....