是否可以在 Silverstripe 模板变量上运行函数来格式化输出?

发布于 2024-10-15 22:51:11 字数 159 浏览 7 评论 0原文

我创建了一个数据模型,其中包含办公室地址的纯文本区域输入字段。在我的相关 Silverstripe 模板中打印数据时,我想执行与 nl2br($OfficeAddr) 相同的操作。据我所知,他们的模板系统不支持此类功能。

我错过了什么吗?有什么推荐的解决方法吗?

I've created a data model that includes a plain textarea entry field for an office address. I would like to do the equivalent of nl2br($OfficeAddr) when printing the data in my relevant Silverstripe template. As far as I can tell, their templating system does not support such functionality.

Am I missing something? Any recommended workarounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦幻之岛 2024-10-22 22:51:11

在 Silverstripe 3 中,最好通过创建 DataExtension(如反对覆盖该类)。 (注意:这在 2.4.x 中也是可能的,但代码会完全不同。)

创建一个名为 TextFormatter 的新类,它扩展了 Extension

class TextFormatter extends Extension { 
    public function NL2BR() {
        return nl2br($this->owner->value);
    }
}

在配置中指定Text 类应该用您的全新类进行扩展。这可以在您的 _config.php 文件或(最好)YAML 文件中完成。

如果您还没有,请在 mysite/_config/extensions.yml 中创建一个包含以下内容的新文件(或者您可以将其附加到现有文件中):

Text:
  extensions:
    ['TextFormatter']

这只是说“扩展Text 类和 TextFormatter 类”,这将使我们的新 NL2BR 函数在所有 Text 对象上可用。

现在,在模板中,您只需调用 $OfficeAddr.NL2BR ,输出将在输出之前通过您的函数运行。

请注意,我假设您的模型使用 Text 作为字段类型,而不是像之前的答案所假设的那样使用 HTMLText 。如果您使用的是 HTMLText,您可以简单地通过适当地更改 extensions.yml 文件来扩展该类。

In Silverstripe 3 this would be best achieved by creating a DataExtension class (as opposed to overriding the class). (Note: this would be possible in 2.4.x as well, but the code would be quite different.)

Create a new class called TextFormatter which extends Extension:

class TextFormatter extends Extension { 
    public function NL2BR() {
        return nl2br($this->owner->value);
    }
}

Specify in config that the Text class should be extended with your brand new class. This can be done either in your _config.php file or (preferably) in a YAML file.

If you don't already have one, create a new file at mysite/_config/extensions.yml with the following content (or you can append this to your existing file):

Text:
  extensions:
    ['TextFormatter']

This just says "extend the class Text with the class TextFormatter" which will make our new NL2BR function available on all Text objects.

Now, in your templates you can simply call $OfficeAddr.NL2BR and the output will be run through your function before being output.

Note that I've assumed your model uses Text as the field type rather than HTMLText as a previous answer has assumed. If you are using HTMLText you can simply extend that class instead by changing your extensions.yml file as appropriate.

时光磨忆 2024-10-22 22:51:11

重要提示:此解决方案适用于 SilverStripe 2.X。如果您使用的是 SilverStripe 3.0 - 请参阅本页上的 SS3.0 答案。

您只需向模型添加一个 getter:

public function FormattedAddress {
    return nl2br($this->OfficeAddr);
}

然后在模板中调用它:

<p>$FormattedAddress</p>

OR - 如果您想遵守 MVC,更复杂的解决方案是...

假设您使用了 HTMLText您可以扩展 HTMLText 类的字段类型:

创建一个名为 - Extended_HTMLText.php (或类似的文件)的文件 - 将以下内容添加到其中并将其保存到您的代码目录中:

class Extended_HTMLText extends HTMLText {
    function NL2BR()    {
        return nl2br($this->value);
    }
}

将以下内容添加到您的 _config.php 文件中:

Object::useCustomClass('HTMLText', 'Extended_HTMLText', true);

然后您可以调用它在你的模板中如下所示:

<p>$OfficeAddr.NL2BR</p>

这至少将你的视图逻辑从你的模型中取出;)

IMPORTANT: This solution is applicable to SilverStripe 2.X. If you're using SilverStripe 3.0 - see SS3.0 answer on this page.

You'd simply add a getter to your model:

public function FormattedAddress {
    return nl2br($this->OfficeAddr);
}

Then call it in your template:

<p>$FormattedAddress</p>

OR - if you want to adhere to MVC, the more complex solution is...

Assuming you've used the HTMLText field type you could extend the HTMLText class:

Create a file called - Extended_HTMLText.php (or something similar) - add the following to it and save it into your code directory:

class Extended_HTMLText extends HTMLText {
    function NL2BR()    {
        return nl2br($this->value);
    }
}

Add the following to your _config.php file:

Object::useCustomClass('HTMLText', 'Extended_HTMLText', true);

Then you can call it in you template like so:

<p>$OfficeAddr.NL2BR</p>

This at least takes your view logic out of your model ;)

尸血腥色 2024-10-22 22:51:11

此问题已在 SilverStripe 3 中修复(自 5 月起) 2013)所有这些答案都早于。现在,所有 TextVarchar 数据库字段都会使用 nl2br() 自动转换。

所以...如果您像我一样愚蠢并且最终到达这里,请注意,您可能实际上正在输出 HTMLText 字段,但认为您正在使用纯文本(因为您可能使用 TextareaField 设置 ->getCMSFields())。

希望这对未来的访客有所帮助!

This has been fixed in SilverStripe 3 (since May 2013) which all of these answers predate. Moving forward now, all Text and Varchar database fields are automatically converted using nl2br().

So ... If you're silly like me and you ended up here, note that there's a possibility that you're actually outputting an HTMLText field but thought you were using plain text (because maybe you setup ->getCMSFields() with a TextareaField).

Hopefully this helps future visitors!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文