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

If != question


Go to End


16 Posts   5606 Views

Avatar
stinkytofu

Community Member, 17 Posts

13 February 2009 at 10:17pm

This seems like a really stupid question, but I cannot find anywhere in the forums or documentation that mentions how to do this.

In the .ss templates, if I want to write something like this:

<% if Result != '123' %>

How do I do it? Right now the above code does not work, and I have to resort to doing something like this:

<% if Result = '123' %>
<!--do nothing-->
<% else %>
<!-- HTML code to display something -->
<% end_if %>

I know this is really simple, but I honestly cannot find anywhere that talks about this. Can someone point me in the right direction?

Thanks!

Avatar
Hamish

Community Member, 712 Posts

14 February 2009 at 1:14pm

Try removing the apostraphies around 123. The templating engine doesn't need them.

Avatar
Fuzz10

Community Member, 791 Posts

15 February 2009 at 12:45am

Hi , welcome to SS..

The template engine is very basic. It doesn't do "NOTS" .. ;-)

Avatar
stinkytofu

Community Member, 17 Posts

15 February 2009 at 1:42am

Thanks Fuzzio. That's what I figured. But I did find in the template documentation (http://doc.silverstripe.com/doku.php?id=templates) example code as follows:

<% if Property != value %>
<% end_if %>

I tried this, but it didn't work. So, I simply figured that the only way to do != is to do this

<% if Property = value %>
<!-- do nothing -->
<% else %>
<!-- do something -->
<% end_if %>

This is really the reason I dislike SilverStripe's templating engine. It ties the programmer's hands and forces us to learn a whole new scripting language syntax. While I absolutely love SilverStripe, my biggest gripe with it is that I am forced to use this extremely limited template scripting language inside the HTML code. Why not give me the option of directly inserting PHP code into the template? I've always avoided templating engines like Smarty, because I don't find the customised templating code to be any easier to read or write than PHP code. PHP is a very well known language and has a huge community of support behind it, so why force programmers to learn and use an entirely new non-standard language when PHP works perfectly well as a templating language? The other issue I have with templating engines is speed. Forcing PHP to parse through the HTML and interpret the template code is much slower than embedding PHP directly into the template - more code to parse means slower sites. I understand you want to prevent bad programmers from inserting PHP business logic code into the templates and creating spaghetti code, but I often insert PHP code into my template simply for debugging purposes and then remove it later. Why should SilverStripe care if programmers want to write spaghetti code? Just let us code the way we want to. If a programmer wants to insert SQL code into their view layer, then let them. This is an educational issue, if separating MVC is important in my company, I'd rather educate our programmers to learn how to separate code into MVC, rather than force them to learn a new non-standard scripting language in order to enforce the MVC architecture. PHP is a templating language in itself, we don't need to learn another one. Those of us who want to follow the MVC framework will do so, and for those who don't care, why force them? A good tool doesn't force people to do things in one certain way, a good tool is flexible enough to allow people to accomplish things the way they want to accomplish them. And that's my only rant about SilverStripe...

Also, I am wondering if it is possible to check for NULL values in the SIlverStripe templating language - something like this:

<% if Property = NULL %>

But I can't get it to work, and have to revert to using something like:

<% if Property %>
<!-- do nothing -->
<% else %>
<!-- do something -->
<% end_if %>

Anybody know a way to do this?

Avatar
Carbon Crayon

Community Member, 598 Posts

15 February 2009 at 2:59am

Edited: 15/02/2009 3:00am

Hi stinkytofu

You could you can extend the template functions in your model to do these sorts of things, for example for a != you could do:

function NotEqual($Var1, $Var2){

if($this->{$Var1} != $Var2){
return true;
}else{
return false;
}

}

then just pass in what you need to <% if NotEqual(Result, 123) %>

It has to be in the model though (i'd stick it in Page if you want to use it a lot) so that you can access it from other pages, so I guess that goes slightly agains the MVC approach but it works! You could do something similar to check if its null.

For me the major limiting factor of the templating engine is that you can't pass variables. If you could that would pretty much allow you to do anything.

Avatar
Fuzz10

Community Member, 791 Posts

15 February 2009 at 5:45am

Those are valid points StinkyTofu.. There are some very good arguments _FOR_ using a system like this as well.

Personally , I did not really see the point of the template engine at first. But after using Silverstripe for some time , I can't say it really bothers me anymore. There isn't really much of a different syntax to learn <% control %> <% if %> <% include %> , that is about it .. ;-)

It keeps stuff very clean without thinking too much ... I like that. lol...

Here are some interesting threads on the dev-group:
http://groups.google.com/group/silverstripe-dev/browse_thread/thread/718c3d9e1454d344/668d294cfa9a3194?q=template&lnk=ol&;
http://groups.google.com/group/silverstripe-dev/browse_thread/thread/db752c5de80f3156/da860dea63988373?lnk=gst&q=template#

Avatar
stinkytofu

Community Member, 17 Posts

17 February 2009 at 7:02pm

I still don't see the point of the templating engine. You can still keep stuff clean by using PHP code instead of the non-standard templating code if you wanted to, you can do it just as easily, if not easier.

Honestly, what's the real difference between using:

<? if ($name == 'bubba') ?>
vs.
<% if name = 'bubba' %>

Does it really simplify things for the programmer? I can write code that is just as clean using PHP and without having to think too much. Templating engines still don't make sense to me.

Avatar
Carbon Crayon

Community Member, 598 Posts

17 February 2009 at 11:17pm

I think the point is that it keeps it simple by preventing you using complex PHP. It would be a little strange if it somehow allowed you to use certain lines of php and not the rest.

Personally, apart from being able to pass variables, I generally find the template system to be pretty nice, keeps my templates readable even to those who do not know any php. Not sure the same would be true if it allowed me to add php. Also if you could add php to the template it would make the controller pretty useless and therefore break the MVC structure. You could argue that it's up to the developer to know how to keep things MVC, but IMO a good tool stops you making bad choices.

Go to Top