Any PHP -> jQuery libraries out there? - php

Have any bridge libraries been developed for PHP that provide access to the jQuery framework? Ideally it would be nice to have something fairly extensible so that creating jQuery-based content using PHP code would be fairly easy and customizeable. Does such a thing exist yet?

pquery
jqpie
jquery-php
There's a warmup list.

So far I've found one that seems to fit the description. I haven't tried it out yet, so if anyone has any feedback or experience with this or other ones don't hesitate to post!
PQuery

jQPie might be what you're after.
What can jQPie do?
Easily request and process data from php using $.getJSON
Inject php generated html into elements using $.(element).load
Call php functions directly from your web pages using $.jqpie
Call jQuery from php in respond to $.jqpie calls
Advanced autocomplete using jqpie_complete

QueryPath (http://querypath.org) is a full implementation of the jQuery DOM/XML/HTML part of jQuery. QueryPath has full CSS 3 selector support (including the stuff jQuery doesn't have, like XML namespace support). It also comes with DB tools, where you can run queries and have the results inserted into the query object. And it has a template engine, too. Like jQuery, you can write custom extensions very easily.
But it definitely takes advantage of its server-side status.
The main project page is at https://fedorahosted.org/querypath. You can download it there (and see lots of examples, including RSS and SVG manipulation).
Integrating with jQuery, then, can be done easily by sending XML data of many sorts down to jQuery. (You could probably send JSON, too... never tried.) And since the server side code and the client side code both look the same, there's less of a need to learn two totally different toolkits.

Related

Library design - Eliminating dom dependencies

<div id='universals' data-path='/a/' data-load='1'></div>
This is a realy odd case. I have front-end code ( JavaScript ) which knows where it's resources are based upon a path I pass to it via PHP which writes the HTML above.
This is a cool feature that allows me to simply rename the root of my folder on the server and everything keeps working as PHP determines the current working directory and sends it to the client via the code above.
It is a form of reflection.
However I want to build this into an application framework on the front-end.
Now my javascript library, let's call it foo.js is dependent upon a dom element. I'm pretty sure this is not good practice.
My plan it to just make the id settable like this.
foo.setUniverisal('#universal');
and then put in the documentation which attributes are needed.
Any other ideas or better ways to eliminate any kind of dom dependency?
I think the best approach is the simplest one:
foo.setDataPath("pathToData")
Then your client-side "glue" code (one that puts together your library, HTML and PHP) will take a path from your tag and call foo.setDataPath. This will be the most orthogonal design (What is "Orthogonality"?).
Example of situation, where it might be relevant: what if this libary be initialized from the HTML, where this tag cannot be generated. For example, because HTML will be served statically (or via CDN).

What templating languages are suitable for JS and PHP?

I'm looking for a templating "language" that works for both PHP and JS. I had a look at Mustache (has a very limited "if") and a few other like jquery-tmpl-php. So far but none of these seem to fulfil all my criteria:
Works with data provided by JSON (array, map, literal)
Has an "if" statement that can at least check if
a key in a map exists
a list element is first/last/odd/even
a value is equal to a literal
Can iterate over a list (iterating over keys in a map would be a bonus)
The same template and data generates exactly the same result with PHP and JS
Fast enough (I know, it's a bit vague)
Preferably no compiling step
Bonus: a nice way to "pluralize" texts and basically everything that makes i18n easier
Not smarty :)
I appreciate any ideas, suggestions or tips
Thanks,
Marek
I just wrote this whole post up only to re-read your question and see "not Smarty" :P . I'll just say I looked around for a while for a JS and PHP template library and Smarty seemed like the best option for me. You say Mustache is not enough. The only other one that comes to mind with both JS and PHP functionality is Haml.
Original:
I don't have a ton of experience with this yet but I've started using Smarty PHP templates. I create one Smarty template and send JSON to the client for Ajax requests. I then use this JS Smarty engine for client-side processing. If the client does not support Javascript, I can gracefully degrade by just sending the PHP associated array to the Smarty renderer server-side rather than sending it to the JSON parser. Still uses same .smarty file, working fairly well so far and I think I'm going to keep using this methodology for the rest of my project.

How can I manipulate DOM using PHP?

I saw an article here: http://code.lancepollard.com/automatically-publish-posts-to-stumbleupon-with-ruby
I don't know Ruby, but the following lines are pretty self explanatory:
page = agent.get("http://www.stumbleupon.com/submit?url=#{url}&title=#{title}")
form = page.forms.first
form.radiobuttons_with(:name => "sfw").first.check
page = agent.submit(form)
I'm guessing Ruby can fetch the webpage, check a checkbox, then submit the form. Is that possible using PHP?
The Ruby code you referenced actually uses a third party library called Mechanize.
Something similar for PHP is The SimpleTest Scriptable Browser. It's not as feature rich as Mechanize but can get the job done and it can be used independently of the SimpleTest framework.
You probably would want something like:
http://simplehtmldom.sourceforge.net/
PHP's internal support is sufficient, but would be more cumbersome to use than a third party library.
Not out of the box. Possibly there is a third-party library that can do it for you. One that might help is PHPQuery to loop over a fetched page and select the form and its values. The submit then would have to be done using Curl or the like...!
More info:
fetching a page: http://php.net/manual/en/function.file-get-contents.php
JQuery for PHP: http://code.google.com/p/phpquery/wiki/Basics for a basic intro
Submitting a form with Curl: http://davidwalsh.name/execute-http-post-php-curl

Convert HTML page to an image

I want to change my HTML page as an image. Is there a way in PHP to change or save an HTML page as an image?
This is not easy; as NullUserException says in his comment, you would need to render the HTML page on the server-side, which is not something PHP (or any other server-sided language) has built in.
The approach that comes to mind would be to write a program (probably not in PHP, but rather something like C# or C++) that runs on your server, fires up a web browser, and does a series of screen captures (possibly combined with page scrolls). As this is a very nontrivial and bug-prone process, I would suggest looking into third-party components that are capable of doing this.
You would then execute this program from PHP, and when it's done running, display the results from the file it output.
I would advise you to use an external service with an api. This list might be a good start: http://blogs.sitepoint.com/2008/07/10/9-ways-to-put-site-screenshots-in-your-web-app/
Thumbalizr seems great, they allso provide a php script so you can cache the images locally:
http://www.thumbalizr.com/apitools.php
Try taking a look at browsershots.org - source code is available for it if you want to install it locally. Essentially it uses a browser to take screenshots, and can be controlled via an XML-RPC interface, which you can call from PHP.
As others have said this is not a simple job, and not something you can do directly in PHP, so use an external service.
(I'm not affiliated with browsershots.org in any way)

Real time RSS display on web page (best practices and source codes)

i have a php script who parser a rss and give me the data in a know pattern. Im very new with ASP, JavaScript and Jquery so i dont have any idea of how to autoupdate the script and display the new data with a smooth animation (see this example, that exactly what i want). Thanks for the support and if you know a good script to made this i will appreciate it.
Seems like you're looking for this:
http://leftlogic.com/lounge/articles/jquery_spy2/
It's PHP (not ASP), so that might be an issue, though the code is SUPER easy to implement (I've written by own implementation on three separate occasions).
The site itself has some decent documentation on getting things up and running, but if you need some extra help, comment and I'll point you in the right direction :)
Good luck!
The resources people have linked here are helpful and merely mentioning jQuery means you're probably headed in the right direction. But if you're new to this it might still be worth mentioning some of the concepts you'll be looking to play with here.
First of all, you'll probably want to stick with one language on the client side and one on the server side. This means choosing either PHP or ASP -- this isn't clear from your question but I'll assume you're dealing with PHP since that's the language I use for this kind of thing. JavaScript + jQuery is the right choice for the browser (client) side of things.
Like Luca points out, you'll have to set up some JavaScript code that goes live on page load and "polls" the server at a set interval. In JavaScript you do this using something called XMLHttpRequest (or "XHR") and it's pretty complicated. You could use combination of jQuery and a library like the one Matt points to in his answer, or just jQuery -- sample code abounds but it's basically a loop with a function call and sleep timer.
That function call is going to be one of the more difficult parts if you're trying to emulate the Twitter World Cup site. But here's the basic idea: You need to populate a list using jQuery and a data standard like JSON. Since the RSS feed you'll be parsing is written in XML, you'll have to write a server side (PHP/ASP) script that fetches, parses and converts the feed to JSON. In PHP, this is best done through cURL (file_get_contents() if you're lazy), SimpleXML and json_encode(), respectively.
Your JavaScript should load the list based on JSON. To do this, and display any new items, what you'll do is load the JSON from the client (browser) side using a jQuery method like getJSON(). Then you spin through the array object and add any new items to the list by adding new <li> elements to the "DOM." The same jQuery code that does this can easily also do the cross dissolve with something like fadeIn().
It looks like the script on that example page has an Ajax request running every TOT seconds.
You could simply have your PHP script return the RSS data (in JSON format say) and let JavaScript parse it and generate some HTML with it.
If all of this doesn't make sense to you I advice reading a little about JavaScript and PHP... there's plenty of good books.

Categories