Is it possible to run a function on a Silverstripe template variable to format output?

发布于 2022-09-06 08:35:13 字数 372 浏览 46 评论 0

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

我早已燃尽 2022-09-13 08:35:13

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.

惜醉颜 2022-09-13 08:35:13

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 ;)

¢蛋碎的人ぎ生 2022-09-13 08:35:13

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 和您的相关数据。
原文