Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » If $string contains (.ss template)

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

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

Page: 1
Go to End
Author Topic: 1559 Views
  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    If $string contains (.ss template) Link to this post

    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>

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: If $string contains (.ss template) Link to this post

    Hi,

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

    Best regards,

    Pascal

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: If $string contains (.ss template) Link to this post

    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

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: If $string contains (.ss template) Link to this post

    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>

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: If $string contains (.ss template) Link to this post

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

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

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: If $string contains (.ss template) Link to this post

    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

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: If $string contains (.ss template) Link to this post

    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" ';

    }
    }

    ?>

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