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.

General Questions

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

If != question


16 Posts   3125 Views


18 February 2009 at 6:30am Community Member, 17 Posts

I'm sorry, but I think that the lack of something as basic as a 'not equal to" comparison operator or even a simple 'isNull' check in the template code is not really keeping things simple. The fact that I have to write a whole new NotEquals() function inside the model in order to reproduce a very basic != comparison operator can hardly be called simplifying things. And I don't think preventing programmers from using a != in their if statements makes the template code any easier to read than standard PHP code.

Also, I'm not convinced that allowing PHP code inside the template means that the controller will become useless. It simply means that it is possible for the programmer to code in a way that makes the controller useless, but if the programmer wanted to stick with the MVC architecture, they could just as easily write clean MVC code using PHP in the template to control the view, and move any database/business/etc logic into the model/controller. I don't buy the argument that allowing PHP code inside the View will make the templates any less readable, PHP itself is a template language to begin with, why re-invent the wheel? IMHO a good tool is one that is flexible enough to allow you to get things done the way you want and also allows you to become more productive in the process - I don't think templating langauges offer either of these advantages.

Anyway, we could argue about this all day and not come to an agreement. I just want to be clear that I'm not bashing SilverStripe. I really do love this CMS and how easy-to-use it is for the non-technical end user, especially when compared to other CMS like Drupal, Joomla, and even WordPress. They clearly put a lot of thought into the WYSIWYG editor, making it extremely simple for users to create links, insert images, and re-arrange content - something that I've always found lacking in most other CMS systems. I also love how easy it is for a programmer to extend and customise the system. My only gripe is their decision to force the programmer into using an extremely limited, non-standard templating language, instead of simply allowing us to code PHP directly inside the templates.

Well.... actually another gripe I have is how the online documentation is structured - it's quite difficult to find the information you need at times, I also think the search engine for the documentation and forums could be improved. But I will leave this rant for another time ;-)

Otherwise, I am absolutely in love with this CMS and will continue to use it and recommend it to my friends/clients!

Carbon Crayon

18 February 2009 at 6:42am Community Member, 598 Posts

I think you have some valid points, particularly about the !=, I agree that should be in the template system along with a couple of other things (did I mention passing variables? ;) ).
From the point of view of someone who was relatively new to most of the concepts involved when I started learning SS, I think it really helped to know that there was a specific way of working and the templating system played a big part in that. However I can see that if you are an experienced developer, being limited in this way could be frustrating.

Anyway I think we can both agree that SS is generally great :)


18 February 2009 at 9:26am Forum Moderator, 5511 Posts

There is a discussion (or was) about improvements (including !=) to the template parser on the google dev mailing list. It has also been mooted to rethink this whole thing.

Wow that was an old discussion but most points there would still be vaild.


18 February 2009 at 9:22pm (Last edited: 18 February 2009 9:23pm), Community Member, 17 Posts

Okay, here's a problem I just came across with the templates. Maybe I'm just stupid and cannot find an answer in the documentation/forums, but this is what I am trying to accomplish... I have the following code in one of my templates (code has been simplified for the sake of clarity):

<% control Thumbnails %>
<% if NewRow = 1 %>
<% else %>
<% end_if %>
<% end_control %>

NewRow is a function inside the Model. I am trying to compare a value returned by NewRow to see if it is equal to '1' and then create a new row in the table to display the data. Problem is that it seems SilverStripe does not allow me to call the NewRow function from within the <% control %> block. So, it seems that the only alternative for me is to create the TD code inside the Controller and then pass the final HTML code to the View for display. One could argue that this is View logic and shouldn't be put into the Controller, so how would I go about making this work inside the View code? Is there a trick I need to know in order to call the NewRow function from within the Control block?

This is why I dislike templating languages so much, something that should be extremely simple to do with PHP, takes a much longer time to figure out using the template language.

Many thanks!


18 February 2009 at 9:23pm Community Member, 787 Posts

To reach the parent context ... you can use $Top .... e.g. $Top.NewRow


19 February 2009 at 12:22am (Last edited: 19 February 2009 1:49am), Community Member, 17 Posts

Thanks Fuzz10, but it still doesn't work properly. Here is the code:

<% control Children %>
<% if Top.isNewRow %>
<% else %>
<% end_if %>
<% end_control %>

And here is the isNewRow() function code in the Model:

var $counter = 0;

function isNewRow()
if ($this->counter < 5)
return false;

$this->counter = 0;
return true;

This code produced one huge table with one long row, it was not incrementing the counter variable and recognising that a new TR should be written after the 5th record.

I added a line into the isNewRow() function to check if it was being called correctly, something like this:

echo $this->counter;

When I refreshed the page, sure enough, the echo function only outputted the $counter variable's value once, so it seems that isNewRow() is only getting called once, even though it is located inside the Control loop and there is more than one item in the array that is being looped through. I've also tried moving the function into the Model instead of the Controller, as mentioned by someone in the forums (I think that post was actually yours Fuzz10), but that didn't work either. Any idea what could be wrong? How can I make sure that isNewRow is called during each iteration in the loop?


19 February 2009 at 5:57am Community Member, 17 Posts

Okay, I've tried all kinds of possible solutions and have read through a plethora of forum posts and documentation, but it seems there is no way around this issue except by creating a new function inside the controller that grabs the list of the pages from SiteTree and generates the HTML table code. The template will then call this function, which returns the contents of the HTML to display in the page.

This is a pretty ridiculous workaround and once again it is due to limitations with the templating engine. Unless, of course, I'm an idiot and I've completely overlooked something here and I really do hope I have missed something - because this is a lot of work for something that should be very simple to do - so somebody please tell me what an idiot I am and point me down the right path. If I haven't overlooked anything, then this is a perfect example of why forcing us to use a template engine instead of enabling us to code PHP directly inside the templates results in many headaches for programmers and with the workaround I have to use now, I am not even following the MVC architecture that the template engine is supposed to help me to do. Please, for the love of god, let us use PHP inside the templates! I beg you! It's not only for my own selfish reasons, but from the perspective of the SilverStripe developers, allowing PHP in the template means that you will no longer have to deal with anymore posts from idiots like myself asking you stupid questions about how to use the template engine and this will free up your time to answer other much more interesting questions as well as add cool new features to the CMS :-)


19 February 2009 at 10:06pm Community Member, 787 Posts

Well yeah, this is a good example of the template system being too simple. I once had a similar situation and solved it by iterating over the dataobjectset and modifying it with meta-data by using the customise() function. This is pretty dirty as well but at least prevents you from spitting out HTML in your controller.

I agree the current template engine is a bit too simple.....

However, when working in teams where different people have different responsibilities I love the cleanliness and ease of use of a structured template system....