Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

If $string contains (.ss template)


Go to End


7 Posts   3775 Views

Avatar
DeklinKelly

Community Member, 197 Posts

26 May 2009 at 9:58pm

Here is the code from my .ss template:

<a href="$QuotationLink.ATT" title="$QuotationAuthor.ATT">$QuotationAuthor.ATT</a>

if $QuotationAuthor contains the text red then I want this:

<a href="$QuotationLink.ATT" title="$QuotationAuthor.ATT" class="red">$QuotationAuthor.ATT</a>

else I want this:
<a href="$QuotationLink.ATT" title="$QuotationAuthor.ATT" class="green">$QuotationAuthor.ATT</a>

Avatar
PGiessler

Community Member, 47 Posts

27 May 2009 at 12:01am

Edited: 27/05/2009 12:01am

Hi,

I don't know what you mean? Give me an example for the $QuotationAuthor variable.

Best regards,

Pascal

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 May 2009 at 12:22am

Edited: 27/05/2009 12:24am

Are you looking for something like this?

<a href="$QuotationLink.ATT" <% if QuotationAuthor== "red" %>class="red"<% else %>class="green"<% end_if %>>$QuotationAuthor.ATT</a>

Ben

Avatar
DeklinKelly

Community Member, 197 Posts

27 May 2009 at 12:23am

If $QuotationAuthor is 'John Red'

Then I want my link to be like this:
<a href="#" title="John Red" class="red">John Red</a>

If $QuotationAuthor is 'Bob Frank'

Then I want my link to be like this:
<a href="#" title="Bob Frank" class="green">Bob Frank</a>

Avatar
DeklinKelly

Community Member, 197 Posts

27 May 2009 at 12:25am

<% if QuotationAuthor== "red" %> will NOT work because it checks if $QuotationAuthor EQUALS "red".

I want to test if $QuotationAuthor CONTAINS 'red' anywhere in it.

Avatar
PGiessler

Community Member, 47 Posts

27 May 2009 at 12:29am

You can write a function which checks the $QuotationAuthor. If the function detects the "red" then $output = red | else $output="green". You have to write this in the PHP-File and not in ss-Template-File.

Best regards,

Pascal

Avatar
DeklinKelly

Community Member, 197 Posts

27 May 2009 at 1:11am

Thank you for pointing me in the right direction. This works! I used a custom text parser.

<?php

class checkSelected extends TextParser
{
   function parse(){
   $str = $this->content;

   if(strpos($str,'red')!== FALSE) 
   return ' class="red" ';
   else return ' class="green" ';

   }
}

?>