Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Adding text formating methods

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1276 Views
  • JL
    Avatar
    Community Member
    10 Posts

    Adding text formating methods Link to this post

    What's the easiet way to add text formatting methods like URLEncode, Shorten to the fields, so that I can use $Title.URLEncode in the template, similar to $Title.XML. Thanks!

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Adding text formating methods Link to this post

    how about - create a subclass of Text, MyText, and add the required methods to it, then change your $db from Text to MyText
    note - totally untested but that is how I'd go about it. Maybe a decorator would work also...

  • Lucas
    Avatar
    Community Member
    10 Posts

    Re: Adding text formating methods Link to this post

    Add a decorator if it's just a method or two that you want to be available on all objects of the given type.

    For example:

    Create the decorator code (say in mysite/code/MyDateDecorator.php):

    class MyDateDecorator extends DataObjectDecorator {
    public function Datetime() {
    return date('Y-m-d\TH:i:s', strtotime($this->owner->value));
    }
    }

    In mysite/_config.php, enable the extension:

    Object::add_extension('SS_Datetime', 'MyDateDecorator');

    Then in your templates you can use it as below:

    <% control MyObject %>
    <h2>$Title</h2>
    <p>Created on <time datetime="$Created.Datetime">$Created.Nice</time></p>
    <% end_control %>

    1276 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.