Jump to content

when do we need curly brackets?


wishbone
 Share

Recommended Posts

non coders get sick with stupid simple problems like when to use curly brackets and when not.

Is it php syntax or processWire syntax?

I found:

http://processwire.com/talk/topic/2682-drop-down-select-to-search-form/?p=26224

"Curly brackets only necessary if you've got two "->" in there"

but

http://processwire.com/talk/topic/1627-newbie-question-on-template-tags/?p=14916

<li><a$class href='{$child->url}'>{$child->title}</a></li>

only one "->" in there...

and

http://processwire.com/api/variables/pages/

$skyscrapers = $pages->get("/cities/chicago/")->find("floors>=60, sort=floors");

two "->"s but no curlies...

two hours trying to find out and no result...

this is what I want to do:

if($section->knots) {
	echo "<tr>";
	echo "<td><p>$pages->get('/FixedData/')->knotsLang:</p></td>";
	echo "<td><p>{$section->knots}</p></td>";
	echo "</tr>";
	}

 

  • Like 1
Link to comment
Share on other sites

First thing - while you might not need curly brackets, you can add them and it won't hurt.

if($section->knots) {
	echo "<tr>";
	echo "<td><p>{$pages->get('/FixedData/')->knotsLang}:</p></td>";
	echo "<td><p>{$section->knots}</p></td>";
	echo "</tr>";
	}

Note the brackets added in the third line - should work (untested).

if($section->knots) {
	echo "<tr>";
	echo "<td><p>{$pages->get('/FixedData/')->knotsLang}:</p></td>";
	echo "<td><p>$section->knots</p></td>";
	echo "</tr>";
	}

Also should work - without the curly brackets in line 4.

If in doubt (and this is probably not good practice, but I often do it) - 

if($section->knots) {
        $knotsLang = $pages->get('/FixedData/')->knotsLang;
	echo "<tr>";
	echo "<td><p>$knotsLang:</p></td>";
	echo "<td><p>$section->knots</p></td>";
	echo "</tr>";
	}

Which (to me, at least) looks a bit cleaner and easier to read.

  • Like 1
Link to comment
Share on other sites

Curly brackets are a PHP thing, not specifically PW.

This is an example I am using right now. I am in a foreach loop which is "$data as $np". $np contains a "page_template" item. I want to get the template from the $templates variable that matches the name returned by $np->page_template. If I didn't have the curlies, it would treat $np and page_template separately and wouldn't work. I think basically you can say that the curly brackets evaluate what is inside first.

$templates->{$np->page_template}

That is a terribly worded explanation, but hopefully it makes some sense :)

  • Like 2
Link to comment
Share on other sites

non coders get sick with stupid simple problems like when to use curly brackets and when not.

Ay - I here you there brother.

It's a coded jungle out there with brackets, curly brackets, comma's, semi columns, double quotes, single quotes, double points, forward slash, backward slash, arrows, white space, etc. etc. And then all that is going to be mixed with php, api, html, apart from css and js.

  • Like 3
Link to comment
Share on other sites

but

http://processwire.com/talk/topic/1627-newbie-question-on-template-tags/?p=14916

<li><a$class href='{$child->url}'>{$child->title}</a></li>

only one "->" in there...

and

http://processwire.com/api/variables/pages/

$skyscrapers = $pages->get("/cities/chicago/")->find("floors>=60, sort=floors");

two "->"s but no curlies...

You only need curly braces when using variables inside strings, like here "<li><a$class href='{$child->url}'>{$child->title}</a></li>". In your second case you only use PHP when giving the value to the variable, there's no string so you don't have to use curly braces.

edit: also, using the curly braces, even if only for a simple variable "I had {$number} coffees today", makes it more readable.

  • Like 4
Link to comment
Share on other sites

You only need curly braces when using variables inside strings, like here "<li><a$class href='{$child->url}'>{$child->title}</a></li>".

In your second case you only use PHP when giving the value to the variable, there's no string so you don't have to use curly braces.

Good Post. Never read it so clear. Gives me a good handle on those matters.

  • Like 1
Link to comment
Share on other sites

You only need curly braces when using variables inside strings, like here "<li><a$class href='{$child->url}'>{$child->title}</a></li>". In your second case you only use PHP when giving the value to the variable, there's no string so you don't have to use curly braces.

what string??

pls talk plain text to my curly brain...

Link to comment
Share on other sites

what string??

pls talk plain text to my curly brain...

No offence intended but, If you don't have a grasp of what a string is in PHP then you have a bigger problem than curly brackets :D

Jokes aside, a string is one of the data/variable types in PHP...others are integers, floats....

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

as I said I'm a non coder...

I have a glance of what is a string, but where is there a php string??? there is some html...

So, to my opinion, it's almost not possible for a non coder to use PW and the target audience never would come from wordpress

Compared to other designers, I do a lot of coding and never give up before I have the solution - and even for me, it's exhausting. How would it be for "normal" designers???

Without all the help here which is really unusual, I wouldn't be able to  do a single thing in PW!

  • Like 1
Link to comment
Share on other sites

I suppose horses for courses.....there's a tool for everyone :)....I like your determination though...you've not given up and are willing to learn. I like the site you recently created. On the other hand, reading up on the basics of PHP does help. I'm talking from experience..... :D

This is an example PHP string..."I am a string. I don't have curly hair though, sorry. In fact, I don't have any hair...hence the hat"; It will be output as is without any evaluation....i.e. literally..

  • Like 1
Link to comment
Share on other sites

So, to my opinion, it's almost not possible for a non coder to use PW and the target audience never would come from wordpress

Compared to other designers, I do a lot of coding and never give up before I have the solution - and even for me, it's exhausting.

How would it be for "normal" designers??? Without all the help here which is really unusual, I wouldn't be able to  do a single thing in PW!

Yes that's exactly how it is. I am going through the same phase. Not born with a programmers/coders mind but mad about processwire. It's a hard fight. But lots can be done with only the api of processwire. Stick to that first and in the mean time learn php to it. Works for most designers. And yes thanks to this forum with a lot of helping pro coders things are really good here.

  • Like 2
Link to comment
Share on other sites

so, if the above example 2 would be inside "echo", it would need some curls?

like so:

echo "{$skyscrapers = $pages->get('/cities/chicago/')->find('floors>=60, sort=floors');}";

wel doesn't really make sense, but anyway just for learning

Link to comment
Share on other sites

so, if the above example 2 would be inside "echo", it would need some curls?

like so:

echo "{$skyscrapers = $pages->get('/cities/chicago/')->find('floors>=60, sort=floors');}";

wel doesn't really make sense, but anyway just for learning

Sometimes it is best just to try and see but check for weird results (e.g. when doing some math). That's how I learnt about curly braces. Sometimes I output something and nothing appears, I add curly braces and voila, it works. So, feel free to try :)

Link to comment
Share on other sites

Sometimes it is best just to try and see but check for weird results (e.g. when doing some math). That's how I learnt about curly braces. Sometimes I output something and nothing appears, I add curly braces and voila, it works. So, feel free to try :)

HAHAHA!

But I'm the kind of type who wants to know what I'm doing and why!!

Naturally, for a non coder it's mostly try and error...

by the string way: DaveP ommitted the curlies in line 4. But there _is a variable inside a string - my brain goes really curly now :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...