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.

Template Questions /

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

Compare two values


Go to End


4 Posts   1591 Views

Avatar
Tread

Community Member, 8 Posts

20 March 2015 at 1:01am

Hello community,

I try to compare two dynamic values in a template, but it doesn't work.
Here is the code:

<select name="agents" class="dropdown" id="Form_showForm_agents">
	<% loop $getAgents %>
		<option value="$key" <% if $getAgent() == $value %>selected="selected"<% end_if %>>$value</option>
	<% end_loop %>
</select>

Even though at one time getAgent() and $value should match, they don't, I checked that.
The Agent names contains a space character, could that be the problem?
When I compare to a static value on a different occasion, say $getEntry() == 10, it works.
What am I doing wrong?

Avatar
Pyromanik

Community Member, 419 Posts

21 March 2015 at 12:23am

Edited: 21/03/2015 12:28am

Hello Tread,

I think the problem here is that you're a bit confused about the domain of logic and the templating language as a whole.
The template language is NOT php, and is simplistic by design in order to keep logical operations as abstracted as possible (in their appropriate php classes, where they belong).

A simple equality evaluation is usually possible though. So part of the issue is likely just syntactical - as this is not php, the parentheses are not required (if you are not passing any parameters).
If a reference turns out to be an executable, the template processor will execute it to get the value.

You simply need $getAgent or even just $Agent (the 'get' part is __magic).

I cannot say for sure without getting some hints on your data structure, but it looks like you're trying something along the lines of:

function getAgent() { return $anObject; }
function getAgents() {return $keyedArray; }
foreach(getAgents() as $key => $value) {
	if($value == getAgent())
		//...
}

Which has several problems with it, because ViewableData goes into (is supplied to) the template, again, not php.
If my above assumption is (loosely) correct, you probably also need $Up or $Top, making $Up.Agent to break out of the loop scope.

Loop iterates through a list like foreach, but unlike foreach it also changes SCOPE to the current item (sort of like this in javascript, most notable in jquery collections)
It seems likely by the names of your variables that getAgent is probably redundant as you should be on the current agent as per the list returned by getAgents.
As just explained, each iteration you are in the scope of that agent already.
This would mean that $key and $value are properties of the $Agent (ie, $Agent->key / Agent.value)

To me it makes more sense on say, if I'm signed in as an Actor or something viewing a page that lists agents

<% loop $Agents %>
	<% if $Me == $Top.MyAgent %>
		...
	<% end_if %>
<% end_loop %>

Where $Me is analogous to $this in php.

Hope that clears things up a bit (again, all provided my wild assumptions are correct. if not, I hope they're understandable enough to be ok examples).

Avatar
Tread

Community Member, 8 Posts

23 March 2015 at 11:40pm

Edited: 23/03/2015 11:41pm

Thanks a lot Pyromanik, that helped, it now works.

I'm using DataObjects in an ArrayList for the getAgents.
Maybe too much, but I wasn't shure if it would loop over a simple array.

$arList = new ArrayList();
foreach ($result as $key => $value)
{
	$item = new DataObject();
	$item->key = $key;
	$item->value = $value;
	$arList->push($item);
}

Whereas getAgent just returns a string.

It now works with this code:

<% loop $getAgents %>
	<option value="$key" <% if $Me.value == $Top.getAgent() %>selected="selected"<% end_if %>>$value</option>
<% end_loop %>

It also works, with just $value, but the $Top has to be in front of the getAgent().

Thanks a lot.

Avatar
Pyromanik

Community Member, 419 Posts

25 March 2015 at 11:26am

You're welcome, man!