pinkp
pinkp

Reputation: 455

SilverStripe 3.7: How to render template syntax or PHP output with out spaces?

How can I remove spaces output in my template? For example I have a $Brand some of these output as two words as the are in my php variables as TommyHilfiger. Output becomes "Tommy Hilfiger". This is great for front end display but how can I render it as TommyHilfiger or Tommy-Hifiger? I want to use them as css classes in my html. Like $Brand.Nospaces for example. Or does it need to be done in the PHP?

PHP

class ProductPage extends Page {

    // Contact object's fields
    public static $db = array(
        'Brand' => 'Varchar(255)'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab("Root.Details", 
            new DropdownField("Brand", "Brand", 
                array( 
                    "Adidas" => "Adidas",
                    "AmericanSportsTeams" => "American Sports Teams",
                    "United Colors of Benetton" => "United Colors of Benetton",
                    "Valentino" => "Valentino",
                )
            )
        );

        return $fields;
    }
}

class ProductPage_Controller extends Page_Controller {

}

Template ProductPage.ss

<% loop Children %>
    <li class="$Brand">
        <a href="$Link">
            <figure style="background-image: URL(<% loop $ProductImages.limit(1,1) %>$Fill(400,600).URL<% end_loop %>);">
                <img src="<% loop $ProductImages.limit(1) %>$Fill(400,600).URL<% end_loop %>" alt="$Title" 
                    class="snipcart-add-item"
                    data-item-id="P$ID $MenuTitle"
                    data-item-max-quantity="1"
                    data-item-stock="1"
                    data-item-name="$Title"
                    data-item-price="<% if $SalePrice %>$SalePrice<% else %>$Price<% end_if %>"
                    data-item-description="$Content"
                    data-item-image="<% loop $ProductImages.limit(1) %>$Pad(50,50).URL<% end_loop %>">
            </figure>
            <div id="pro-deets">
                <h3>$Title</h3>
                $Brand
            </div>
        </a>
    </li>
<% end_loop %>

Maybe with:

$filter = URLSegmentFilter::create();
$className = $filter->filter($title);

I'm just not clear how to apply this to $Brand it would need to put in the template as $BrandNoSpace or something as I need to use $Brand with its spaces too for the displayed txt.

Upvotes: 3

Views: 670

Answers (5)

Gavin Bruce
Gavin Bruce

Reputation: 1809

This is pretty much a duplicate of Display Multi Word Categories As 1 Word With Dashes

You can use an extension for this. This means that this method will be available for any varchar field.

mysite/code/extensions/VarcharDecorator.php

<?php
class VarcharDecorator extends Extension {
    function Slugify() {
        return FileNameFilter::create()->filter(trim($this->owner->value);
    }
}

mysite/_config/extensions.yml

Varchar:
  extensions:
    - VarcharDecorator

Now you can use $Brand.Slugify in your templates.

Upvotes: 1

3dgoo
3dgoo

Reputation: 15794

We need to create a PHP function to convert our string as there is no inbuilt way to do this in a Silverstripe template.

Silverstripe has an inbuilt URLSegmentFilter class that strips certain characters from a string. We can use this to convert our string to a class name friendly string.

We can create a getBrandClass function in our ProductPage class to convert the Brand to a nice string with no spaces or special characters:

class ProductPage extends Page
{
    public static $db = [
        'Brand' => 'Varchar(255)',
    ];

    // Other ProductPage class code
    // ...

    public function getBrandClass()
    {
        $urlSegmentFilter = URLSegmentFilter::create();
        return $urlSegmentFilter->filter($this->Brand);
    }
}

Then in our template we call $BrandClass like this:

<% loop $Children %>
    <li class="$BrandClass">

This will convert American Sports Teams to american-sports-teams.

Note that the getBrandClass function must go in the ProductPage class and not in the ProductPage_Controller class. This is because in the template a different controller is being used.

Upvotes: 2

patJnr
patJnr

Reputation: 174

A custom function in your controller will be much easier for what you want.

class ProductPage_Controller extends Page_Controller
{
    public function BrandNoSpaces()
    {
        return str_replace(' ', '-', $this->Brand);
    }
}

In your template:

<li class="$BrandNoSpaces">

'United Colors of Benetton' becomes

<li class="United-Colors-of-Benetton">

Upvotes: 2

spekulatius
spekulatius

Reputation: 1499

One simple approach would be to modify the string in the SilverStripe controller:

class ProductPage_Controller extends Page_Controller
{
    /**
     * returns the brand name as a class (with dash instead of space)
     *
     * @return string
     */
    public function BrandAsClass()
    {
        return str_replace(' ', '-', $this->Brand);
    }
}

and then call $BrandAsClass in your template:

<% loop Children %>
    <li class="$BrandAsClass">
        <a href="$Link">
        <figure style="background-image: URL(<% loop $ProductImages.limit(1,1) %>$Fill(400,600).URL<% end_loop %>);">
            <img src="<% loop $ProductImages.limit(1) %>$Fill(400,600).URL<% end_loop %>" alt="$Title" 
            class="snipcart-add-item"
            data-item-id="P$ID $MenuTitle"
            data-item-max-quantity="1"
            data-item-stock="1"
            data-item-name="$Title"
            data-item-price="<% if $SalePrice %>$SalePrice<% else %>$Price<% end_if %>"
            data-item-description="$Content"
            data-item-image="<% loop $ProductImages.limit(1) %>$Pad(50,50).URL<% end_loop %>">
        </figure>
        <div id="pro-deets">
        <h3>$Title</h3>
         $Brand
        </div></a>
    </li>
<% end_loop %>

Upvotes: 2

Nils M.
Nils M.

Reputation: 76

If you just want to remove spaces from the beginning or end of a string use

strip($string)

if you want to remove all spaces use

str_replace(' ', '', $string)

if you want to change a space to a -

str_replace(' ', '-', $string)

if you want to remove all possible white spaces use

preg_replace('/\s+/', '', $string)

Upvotes: 1

Related Questions