Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Get jQuery in PHP with pQuery

Tuesday, February 3rd, 2015 by Servage

html-domIf you are familiar with jQuery you know the great and easy way to work with DOM elements based on selectors. You start by selecting the elements you are interested in, whereafter you can perform calculations, manipulations and various other tasks with them. This is a highly flexible and efficient way to combine JavaScript and HTML for dynamic effects on a website.

Since JavaScript is executed in the client’s browser, you cannot use jQuery in your PHP backend, yet some developers would like a similar easy way to handle HTML there. The HTML generation and manipulation often becomes a bit messy in PHP. There are plenty of libraries that try to aid that process, but nothing is really as nice as the way jQuery handles it in JavaScript.

Selector based HTML handling in PHP

There are a couple of libraries which make it possible to work with HTML based on CSS selectors like jQuery. Unfortunately their development seems a bit outdated and stagnated. Their features are also not as extensive as jQuery. Two of the better (but old) projects are:

Both of the above are good starting points, but they rely on the traditional way of object oriented PHP programming. Obviously that is principally a good thing when working with PHP, but to really get the benefits like jQuery you need chainable objects. Thankfully this is possible with pQuery, a library based on the Ganon Parser wrapped in chainable methods.

Getting pQuery

It is a Composer package that needs to be included with your project. If you are not yet familiar with Composer, then please have a look at their website. It offers a clever way to include and manage dependencies in PHP projects using packages.

Using pQuery for DOM handling

Start by parsing HTML content either from a string or from a file:

// Parse from string
$html = pQuery::parseStr('... some html ...');

// Parse from file
$html = pQuery::parseFile('... some file ...');

Now you have a pQuery object in the $html variable which you can work with. Notice that the object is chainable like jQuery objects in JavaScript:

// Get all p-elements inside containers with
// the "description" class and echo their text content.
echo $html->query('.description p')->text();

Fortunately pQuery also has similar capabilities as jQuery when it comes to manipulating DOM elements, and not just to find or select them. This is useful for building HTML and can make the creation of HTML content with PHP neater and more comprehensible for the developer.

The following example shows how you can build and change HTML with pQuery:

// Create a HTML object with a basic div container
$div = pQuery::parseStr('<div></div>');

// Add some CSS class to the div
$div->addClass('container');

// Add some CSS classes in a chain
$div->addClass('class1')->addClass('class2')->addClass('class3');

// Insert content into the div
$div->html('Inner html of the element ...');

There are plenty of methods available to build the HTML you need.

Note that pQuery implements an interface called IQuery, so you could actually do the same thing with a different parser than the Ganon project, if you need.

Get jQuery in PHP with pQuery, 5.0 out of 5 based on 2 ratings
Categories: Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.