Live data from Hacker News

XHP: A New Way to Write PHP (from Facebook)

facebook.com

11–20 of 72 posts

Re: XHP: A New Way to Write PHP (from Facebook)

#11

XHP rocks so fucking hard, it isn't even funny. It is just so much better than alternatives. IMHO, It is the only PHP tool I use at facebook that is better than alternatives in other languages. I'm looking at you, django templates! The notation perfectly represents the objects, with no cruft associated with object oriented programming. That is really rare. You could argue that the markup syntax is cruft, but it reall…

Couple questions:

1. How is this different from Django filters? Is it that the default is HTML escaping instead of having to specify the escaping with each template variable?

2. How does it handle different escaping contexts? For example, text in html attributes needs to be escaped differently from text in the body of the document. Text in URLs or JavaScript has to be escaped differently still, and often times you have to combine these escapings (eg. a JavaScript onClick attribute). Is XHP smart enough to recognize these different contexts and do the right thing, or do you need to fall back to some manual mechanism?

Re: XHP: A New Way to Write PHP (from Facebook)

#12
post #8

seems to me that if they went to the trouble to make it understand xml syntax and error out on invalid code, they should have just made it auto-close tags. on pages where there are heavily nested divs and other things, auto-closing the tags would make the code smaller while still generating valid xhtml and not bothering the developer with such trivial things. echo blah;

While I understand where you're coming from, I feel like that would result in incredibly confusing markup and only lead to increased headaches down the road. I'm glad they left that out.

Re: XHP: A New Way to Write PHP (from Facebook)

#13
post #5
post #3

That is, it is impossible to generate malformed webpages while using XHP. While the purist in me thinks this is great if everyone else uses it, the immense amount of productivity lost when I first started using kid templating (e.g. http://turbogears.org/about/kid.html ) really burned me on this whole concept. Sometimes I really do want to make a quick test page without crossing all my i's and dotting all the t's. Imp…

Saving 14 bytes per request is something only a very few sites need to think about... For the rest of us, clarity and semantically correct code are much more useful.

Wasn't there just a thread complaining about how the end tags make HTML so much more verbose and difficult to read, and that's why people are writing preprocessors like HAML and XHP? I've found that the code is much clearer when you omit your end tags.

As for semantic correctness - it's in the HTML spec, and every major modern browser handles it correctly. Sometimes I wonder if Google's the only folks who actually read the W3C specs, there's been so much cargo-cult advice passed down between web developers.

The "always close your tags" advice came from the early 2000s, when people were pushing XHTML as a way to make your HTML pages XML compliant (the big buzzword back then). It gives essentially no benefit to users, no benefit to developers, costs you bandwidth, makes your pages slower, and clutters up your markup.

Re: XHP: A New Way to Write PHP (from Facebook)

#14

This sure would make working with WordPress themes easier.

The useful abstraction might mean that they wouldn't need to be GPLed, especially if combined with a sane view/template separation (like Django's: the template just gets a dict as input and nothing else).

Re: XHP: A New Way to Write PHP (from Facebook)

#15

XHP rocks so fucking hard, it isn't even funny. It is just so much better than alternatives. IMHO, It is the only PHP tool I use at facebook that is better than alternatives in other languages. I'm looking at you, django templates! The notation perfectly represents the objects, with no cruft associated with object oriented programming. That is really rare. You could argue that the markup syntax is cruft, but it reall…

Couple questions: 1. How is this different from Django filters? Is it that the default is HTML escaping instead of having to specify the escaping with each template variable? 2. How does it handle different escaping contexts? For example, text in html attributes needs to be escaped differently from text in the body of the document. Text in URLs or JavaScript has to be escaped differently still, and often times you ha…

Filters are tags in django templates that live in .html files with a bit of logic in a bespoke mini language. XHP for python would be something like this.

  def view_foo( request ):
    baz = "roger, roger"
    return render_foo( baz )
  
  def render_foo( name ):
    return hi, {name}
The most glaring difference is that instead of template logic and keywords, you can use python. You definitely want to sequester rendering from the rest of your view, but I see little benefit to django templates. Missing from this code sample is some django middleware which renders a proper HttpResponse() from the XHP return.

I don't know well enough to answer about escaping. Check out the framework, and try it for yourself :)

Re: XHP: A New Way to Write PHP (from Facebook)

#16
maybe I've misunderstood, but this seems to advocate mixing inline HTML and php logic - isn't that a huge step backwards in terms of web software architecture? I thought we were all using the MVC model by now...

My head just hurts thinking how utterly unmaintainable all that spaghetti code must be.

Re: XHP: A New Way to Write PHP (from Facebook)

#17
post #5

Earlier quoted context omitted.

Saving 14 bytes per request is something only a very few sites need to think about... For the rest of us, clarity and semantically correct code are much more useful.

Wasn't there just a thread complaining about how the end tags make HTML so much more verbose and difficult to read, and that's why people are writing preprocessors like HAML and XHP? I've found that the code is much clearer when you omit your end tags. As for semantic correctness - it's in the HTML spec, and every major modern browser handles it correctly. Sometimes I wonder if Google's the only folks who actually re…

If you don't close your tags,

foobarbaz box

how can you tell the difference between,

foobarbaz box

and

foobarbaz box

Re: XHP: A New Way to Write PHP (from Facebook)

#18

maybe I've misunderstood, but this seems to advocate mixing inline HTML and php logic - isn't that a huge step backwards in terms of web software architecture? I thought we were all using the MVC model by now... My head just hurts thinking how utterly unmaintainable all that spaghetti code must be.

This doesn't say that at all. The rendering code you write needs to be modular as well, and has quite a bunch of logic built into it (even when all the model and controller logic is separate.) So when you write your renderers as classes or functions, that is when you realize the benefit of XHP.

Re: XHP: A New Way to Write PHP (from Facebook)

#19
For me, XHP is far more interesting than HipHop. And I say that as someone who administers a pile of single-application CPU-bound PHP servers. This completely and forever changes the templates-vs-just-PHP debate, and I'm glad -- it's the kind of evolution PHP needs to continue to be taken seriously.

Re: XHP: A New Way to Write PHP (from Facebook)

#20

maybe I've misunderstood, but this seems to advocate mixing inline HTML and php logic - isn't that a huge step backwards in terms of web software architecture? I thought we were all using the MVC model by now... My head just hurts thinking how utterly unmaintainable all that spaghetti code must be.

Not really. You can still separate MVC style. Templates have always had some display logic, which is ok. What you don't want is intermixed application logic.
Post reply on HN