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

problem with if blocks in templates


Go to End


10 Posts   2244 Views

Avatar
ttyl

Community Member, 114 Posts

11 November 2009 at 5:00am

Edited: 11/11/2009 5:00am

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?

Avatar
Juanitou

Community Member, 323 Posts

11 November 2009 at 6:36am

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

Avatar
ttyl

Community Member, 114 Posts

11 November 2009 at 7:10am

Edited: 11/11/2009 7:14am

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?

Avatar
Juanitou

Community Member, 323 Posts

11 November 2009 at 7:24am

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.

Avatar
ttyl

Community Member, 114 Posts

11 November 2009 at 8:13am

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?

Avatar
ttyl

Community Member, 114 Posts

11 November 2009 at 9:27am

Edited: 11/11/2009 9:28am

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.

Avatar
Juanitou

Community Member, 323 Posts

11 November 2009 at 10:45am

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

Avatar
ttyl

Community Member, 114 Posts

11 November 2009 at 11:02am

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

Go to Top