QueryPath -- jQuery in PHP

Source: Internet
Author: User
QueryPath -- jQuery official homepage http://querypath.org in PHP/

Qp api manual http://api.querypath.org/docs/

The QueryPath (QP) library implements jQuery-like effects in PHP. It can also be used to conveniently process xml html... features are too powerful !!!

A QueryPath Tutorial (A simple description)

QueryPath makes use of method chaining to provide a concise suite of tools for manipulating a DOM.

The basic principle of method chaining is that each method returns an object upon which additional methods can be called. In our case, the QueryPath object usually returns itself.

Let's take a look at an example to explain strate:

                          

$ Qp = qp (QueryPath: HTML_STUB); // Generate a new QueryPath object. (create a QP object) $ qp2 = $ qp-> find ('body'); // Find the body tag. (Find the "body" tag) // Now the surprising part :( Please refer to the following to surprise) if ($ qp ===$ qp2) {// This will always get printed. (it always outputs like this) print "MATCH ";}

Why does $ qp always equal $ qp2? Because the find () function does all of its data gathering and then returns the QueryPath object.

This might seem esoteric, but it all has a very practical rationale. With this sort of interface, we can chain lots of methods together:

(You can use the same concatenation method as jQuery)

                      

                              

qp(QueryPath :: HTML_STUB) -> find( ' body ' ) -> text( ' Hello World ' ) -> writeHTML();

In this example, we have four method cballs:

Qp (QueryPath: HTML_STUB): Create a new QueryPath object and provide it with a stub of an HTML document. this returns the QueryPath object. find ('body'): This searches the QueryPath document looking for an element named 'body '. that element is, of course,Portion of the HTML document. when it finds the body element, it keeps an internal pointer to that element, and it returns the QueryPath object (which is now wrapping the body element ). text ('Hello World'): This function takes the current element (s) wrapped by QueryPath and adds the text Hello World. as you have probably guessed, it, too, returns a QueryPath object. the object will still be pointing to the body element. writeHTML (): The writeHTML () function prints out the entire document. this is used to send the HTML back to the client. you'll never guess what this function returns. okay, you guessed it. queryPath.

So at the end of the chain above, we wowould have created a document that looks something like this:

                          

< html xmlns ="http://www.w3.org/1999/xhtml" > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" > < title > Untitled < body > Hello World

Most of that HTML comes from the QueryPath: HTML_STUB. All we did was add the Hello World text inside ofTags.

Not all QueryPath functions return QueryPath objects. Some tools need to return other data. But those functions are well-formed ented in the specified Ded documentation.

These are the basic principles behind QueryPath. Now let's take a look at a larger example that exercises more of the QueryPath API.

A Longer Example

This example extends strates various core features of QueryPath.

In this example, we use some of the standard QueryPath functions (most of them implementing the jQuery interface) to build a new web page from scratch.

Each line of the code has been commented inpidually. The output from this is shown in a separate block.

                          

* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. */ // Require the QueryPath core. require_once ' QueryPath/QueryPath.php ' ; // Begin with an HTML stub document (XHTML, actually), and navigate to the title. qp(QueryPath :: HTML_STUB , ' title ' ) // Add some text to the title -> text( ' Example of QueryPath. ' ) // Now look for the element -> find( ' :root body ' ) // Inside the body, add a title and paragraph. -> append( ' This is a test page

Test text

' ) // Now we select the paragraph we just created inside the body -> children( ' p ' ) // Add a 'class="some-class"' attribute to the paragraph -> attr( ' class ' , ' some-class ' ) // And add a style attribute, too, setting the background color. -> css( ' background-color ' , ' #eee ' ) // Now go back to the paragraph again -> parent() // Before the paragraph and the title, add an empty table. -> prepend( '
' ) // Now let's go to the table... -> find( ' #my-table ' ) // Add a couple of empty rows -> append( ' ' ) // select the rows (both at once) -> children() // Add a CSS class to both rows -> addClass( ' table-row ' ) // Now just get the first row (at position 0) -> eq( 0 ) // Add a table header in the first row -> append( ' This is the header ' ) // Now go to the next row -> next () // Add some data to this row -> append( ' This is the data ' ) // Write it all out as HTML -> writeHTML(); ?>

The code above produces the following HTML:

Code

                              

< html xmlns ="http://www.w3.org/1999/xhtml" > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" > < title > Example of QueryPath. < body > < table id ="my-table" > < tr class ="table-row" >< th > This is the header < tr class ="table-row" >< td > This is the data < h1 > This is a test page < p class ="some-class" style ="background-color: #eee" > Test text

Now you shoshould have an idea of how QueryPath works. Grab a copy of the library and try it out! Along with the source code, you will get a nice bundle of HTML files that cover every single public function in the QueryPath library (no kidding). There are more examples there, too.

Good stuff! Hurry up with Grab ~~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.