Can I run a function in a Silverstripe template variable to format the output?

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

Am I missing something? Any recommended workarounds?

+4
source share
3 answers

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, but the code would be completely different.)

Create a new class called TextFormatter that extends Extension :

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

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

If you do not already have one, create a new file in mysite/_config/extensions.yml with the following contents (or you can add it to an existing file):

 Text: extensions: ['TextFormatter'] 

It simply says, β€œExtend the Text class with the TextFormatter class,” which will make our new NL2BR function available to all Text objects.

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

Note that I assumed that your model uses Text as the type of the field, and not an HTMLText , as the previous answer suggested. If you use HTMLText , you can simply extend this class by modifying the extensions.yml file as needed.

+11
source

IMPORTANT: This solution applies to SilverStripe 2.X. If you are using SilverStripe 3.0 - see SS3.0 answer on this page.

You just added getter to your model:

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

Then name it in your template:

 <p>$FormattedAddress</p> 

OR - if you want to stick with MVC, a more complicated solution ...

Assuming you used the HTMLText field type, you can extend the HTMLText class:

Create a file with the name - Extended_HTMLText.php (or something similar) - add the following code to it and save it in the code directory:

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

Add the following to the _config.php file:

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

Then you can call it in your template as follows:

 <p>$OfficeAddr.NL2BR</p> 

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

+7
source

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

So ... If you are as stupid as I am and you are here, note that it is likely that you will actually HTMLText field, but thought that you are using plain text (because maybe you are setting ->getCMSFields() with a TextareaField ).

Hope this helps future visitors!

+1
source

Source: https://habr.com/ru/post/1338671/


All Articles