需要帮助来实施Orchard CMS Shape方法 [英] Need help implementing an Orchard CMS Shape Method

查看:94
本文介绍了需要帮助来实施Orchard CMS Shape方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习Orchard CMS,在实现shape方法以返回一些任意文本而又不创建整套模型,驱动程序等时遇到麻烦.

I am just learning Orchard CMS and am having trouble implementing a shape method to return some arbitrary text without creating a whole set of model, driver, etc.

我正在尝试使用 http://docs.orchardproject上的代码.净/文档/访问和渲染形状

public class DateTimeShapes : IDependency {
private readonly IClock _clock;

public DateTimeShapes(IClock clock) {
    _clock = clock;
    T = NullLocalizer.Instance;
}

public Localizer T { get; set; }

[Shape]
public IHtmlString DateTimeRelative(HtmlHelper Html, DateTime dateTimeUtc) {
    var time = _clock.UtcNow - dateTimeUtc;

    if (time.TotalDays > 7)
        return Html.DateTime(dateTimeUtc, T("'on' MMM d yyyy 'at' h:mm tt"));
    if (time.TotalHours > 24)
        return T.Plural("1 day ago", "{0} days ago", time.Days);
    if (time.TotalMinutes > 60)
        return T.Plural("1 hour ago", "{0} hours ago", time.Hours);
    if (time.TotalSeconds > 60)
        return T.Plural("1 minute ago", "{0} minutes ago", time.Minutes);
    if (time.TotalSeconds > 10)
        return T.Plural("1 second ago", "{0} seconds ago", time.Seconds);

    return T("a moment ago");
}

但是,我不确定这应该去哪里.我尝试将其放入控制器和模型中,但结果均不成功.

However, I am not entirely sure where this should go. I have tried putting it both in a controller and in a model with unsuccessful results.

所以,我的问题是,此代码应放在哪里?从模板调用它的正确方法是什么?

So, my question is, where should this code be placed? And what is the correct way to call it from a template?

推荐答案

您可以将这段代码放在一个名为Shapes.cs或DateTimeShapes.cs的类中(不是必需的,但与Orchard有点类似)

You can just place this code in a class called Shapes.cs or DateTimeShapes.cs (not necessary, but kind of a convention with Orchard)

然后您可以像这样在视图中使用它:

You can then use it in your views like this:

@{
    var date = DateTime.Now;
}

@Display.DateTimeRelative(date)

这篇关于需要帮助来实施Orchard CMS Shape方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆