Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
pinkp Avatar asked Dec 09 '25 12:12

pinkp


2 Answers

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 %>
like image 58
spekulatius Avatar answered Dec 12 '25 03:12

spekulatius


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">
like image 26
patJnr Avatar answered Dec 12 '25 01:12

patJnr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!