Jump to:

3402 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Best way to convert string variables in templates

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

Page: 1
Go to End
Author Topic: 613 Views
  • Tama
    Avatar
    Community Member
    119 Posts

    Best way to convert string variables in templates Link to this post

    Hello

    A Page Type has the following TextFields:
    $Section1Heading
    $Section2Heading
    $Section3Heading
    $Section4Heading
    $Section5Heading

    I want to be able to display the values of these fields in a template once they have been converted to lowercase and has all non-alphanumeric characters converted to hyphens.

    So "What We Do" becomes "what-we-do" - and can be displayed in the template as such. This is so the converted string can be used as a div id for navigation purposes.

    I can think of a number of different approaches but not sure which one to take.

    Is there a correct or recommended approach for this sort of thing?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Best way to convert string variables in templates Link to this post

    The way I would recommend is a custom getter on your page type. In 3.0 you can pass variables into methods so you only need to define the method once.

    public function getFormattedSectionHeading($heading) {
    $f = URLSegmentFilter::create();
    $t = $f->filter($title);

    return DBField::create('Text', $t);
    }

    Then use $FormattedSectionHeading($Heading) in your template.

    You could also create a new DBField subclass and encapsulate this logic on the field type

    <?php class MyFormattableVarchar extends Varchar {
    public function AwesomeFormat($heading) {
    $f = URLSegmentFilter::create();
    $t = $f->filter($title);

    return DBField::create('Text', $t);
    }
    }

    Then using "Field" => "MyFormattableVarchar" will be possible in $db arrays and your templates will look like $Section1Heading.AwesomeFormat

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Best way to convert string variables in templates Link to this post

    Thank you for your time Will.

    Here's the solution I went for:

    /mysite/code/Model/TidyFormatVarchar.php

    <?php

    class TidyFormatVarchar extends Varchar {

    public function AnchorName() {
    $f = URLSegmentFilter::create();
    $t = $f->filter($this->value);
    return $t;
    }
    }

    /mysite/code/PageTypes/MyPage.php

    class MyPage extends SiteTree {
    public static $db = array(
    'Section1Heading' => 'TidyFormatVarchar',
    'Section2Heading' => 'TidyFormatVarchar',
    ...

    /themes/mytheme/templates/Layout/MyPage.php

    <div id="$Section1Heading.AnchorName" class="section-1">

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Best way to convert string variables in templates Link to this post

    Just for reference, here's an alternative solution I use:

    <?php

    class StringFieldExtension extends Extension {

       public function CSSSafe() {
          return Convert::raw2url($this->owner->value);
       }

    }

    Apply the extension, either YAML:

    StringField:
    extensions:
    - StringFieldExtension

    or _config.php

    Object::add_extension('StringField', 'StringFieldExtension');

    Then in your templates you can use

    <div id="$Section1Heading.CSSSafe" class="section-1">

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Best way to convert string variables in templates Link to this post

    Thank you for posting that up kinglozzer - it's tidier than the approach I used.

    I like tidy.

    613 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.