Jump to:

23372 Posts in 18173 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » problem with if blocks in templates

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 2
Go to End
Author Topic: 1120 Views
  • ttyl
    Avatar
    Community Member
    114 Posts

    problem with if blocks in templates Link to this post

    new to ss having this problem:

    if I try this:

    <% if $something = value %>

    it works, but if I want something longer for value it doesn't

    example:

    <% if $something = longer value %>

    since I can't put 'longer value' in quotes what am I supposed to do?

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: problem with if blocks in templates Link to this post

    Hi!

    I can’t believe your if statement works without removing the $ sign of $something. With respect to the other side of the comparison, SS actually don’t handle “longer values”, neither values containing special character as éñç and the such. You should have to code complex comparisons in your page PHP file.

    Regards,
    Juan

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: problem with if blocks in templates Link to this post

    yeah, I didn't mean to put the '$' sign in there - I had it the correct way in my code.

    I was trying to move the code in question to a function instead (although the idea that an argument can't take a string with a space in it is pretty absurd). following that line of reasoning I tried this which yielded similar results:

       <% control myFunction(whatever) %><% end_control %>

    ...works

       <% control myFunction(what ever) %><% end_control %>

    ...doesn't work

    this is a big problem because I have a field I want to use to sort the children of my page and I don't see how I can pass my string?

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: problem with if blocks in templates Link to this post

    I’m quite bad in PHP, so I cannot help you, but I’m quite sure you can’t make comparisons or pass arguments to functions of the kind you describe.

    I hope it helps.

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: problem with if blocks in templates Link to this post

    thanks for the reply at any rate. but is there a way to include any sort of conditional logic on the page when the parameter is more than a single word? it's such a waste to not be able to pass an argument in that way. unless I can figure out an elegant solution I might have to develop my site in a different CMS and up to now I've been pretty impressed with ss.

    do any devs post on these boards or is that support just for pay?

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: problem with if blocks in templates Link to this post

    Perhaps going into more detail will help me explain my problem in a real example. first, I'll show what I did to get it working - then what I would prefer to do.

    in the controller I have these functions:

       function getProfessors() {
          return DataObject::get( 'FacultyPage', "`FacultyType` = 'Professor'" );
       }

       function getAssociateProfessors() {
          return DataObject::get( 'FacultyPage', "`FacultyType` = 'Associate Professor'");
       }

    on the view I have this

       <p>Professors</p>
    <% control getProfessors %>
          <p><a href="$Link">$Title</a></p>
    <% end_control %>

       <p>Associate Professors</p>
    <% control getAssociateProfessors %>
          <p><a href="$Link">$Title</a></p>
    <% end_control %>

    everything is kosher, but I have many types of professors and not only do I not want to have to write a new function for each one, but I want to be able to handle new kinds without going back to hack my classes. therefore, what I *want* to do is on the controller have this

       function getFaculty($facultytype) {
          return DataObject::get( 'FacultyPage', "`FacultyType` = '$facultytype'" );
       }

    and have this on my view

       <p>Professors</p>
    <% control getFaculty(Professor) %>
          <p><a href="$Link">$Title</a></p>
    <% end_control %>

       <p>Associate Professors</p>
    <% control getFaculty(Associate Professor) %>
          <p><a href="$Link">$Title</a></p>
    <% end_control %>

    note that because the second one is 'associate professor' with a *space* it won't work. is this general type of problem to be solved in another way? I can see it coming up any time I want to sort data on my 'holder' pages.

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: problem with if blocks in templates Link to this post

    You’re not alone, see: http://open.silverstripe.org/ticket/3738

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: problem with if blocks in templates Link to this post

    yes, that would make me very happy!

    I've only been using ss for a week now so I'm not going to venture into testing any new code quite yet...

    1120 Views
Page: 1 2
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.