Welcome Guest, Not a member yet? Register   Sign In
phpQuery and Views
#1

[eluser]Gerson[/eluser]
Hi everyone!

Does anyone try to use phpQuery to fill the content in a View? I'm a beginner and I'd like to use phpQuery to keep the views with pure HTML (no PHP at all) and I'm not sure how is the right (semantic) way to do this.

Any suggestions?
#2

[eluser]TheFuzzy0ne[/eluser]
Just my two cents (and no, I've never used or even heard of phpQuery until today).

phpQuery looks nice, but I like simplicity. I can't figure out exactly what it's supposed to do, but I am interested to know whether or not it outputs valid HTML.

At the end of the day, do whatever makes life easier for you. Personally, I find HTML with a little PHP mixed in, easy to manage. Also, I am happy in the knowledge that there isn't as much overhead to parse the template than there would be with phpQuery, but as I said, whatever makes life easier for you.
#3

[eluser]Gerson[/eluser]
hehehehehe I like your signature...

Well, you're totally right about the "overhead to parse" and really "a little PHP mixed in" is easy to manage. The point here is totally separate the Designer's work and the Programmer's work.

Let's figure it out: phpQuery is a port of jQuery (javascript framework) to PHP. It lets you use PHP to change the HTML DOM without the need of change the HTML file itself, just using CSS selectors or XPath (as jQuery does). You can get more information here: http://code.google.com/p/phpquery/ and a few examples here: http://tobiasz123.wordpress.com/2007/07/...ng-engine/

Now let's say you have a CMS where there are users with assigned profile "Designer", able to edit HTML but not PHP. And users with assigned profile "Programmer" able to edit PHP but not HTML. So the Designer do create valid HTML and care about the design with valid CSS and whatever. But he doesn't know anything of PHP and don't want to learn it. In the other hand there is a programmer who know almost nothing of HTML, nothing of CSS, but needs to integrate the view with the system without broke the layout again.

Using phpQuery the programmer is able to insert the result of his PHP interactions inside the HTML without touch it, just selecting the id="" and class="" values he find on the file.

So, my question actually is how to integrate phpQuery with this: $this->load->view('whateverview');

Thaks TheFuzzy0ne and everyone else for any suggestions.
#4

[eluser]TheFuzzy0ne[/eluser]
Aaaah, now I see! Thanks for the explanation.

I think you could load it as a library easily enough, but you'd probably need to put all the files into a single directory, and put that into your libraries directory. Then you could write a "front end" file, that can be loaded with the CodeIgniter library loader, which will load the other relevant classes, and provide an access point to them (much like the CodeIgniter super object does).

Once that's done, it's just a case of learning the syntax and using it in your controllers.
#5

[eluser]Colin Williams[/eluser]
Best way to implement is probably to overload the Loader::view() method. So, view gets passed the $data array (or object) and you could build in some conventions that phpQuery uses to turn $data properties/values to file manipulations. Say, the class prototype is php_{varname}

View:

Code:
<div class="php_name"></div>

Controller:

Code:
$this->load->view('view', array('name' => 'Pete'));

So, with phpQuery you do something like

Code:
foreach ($data as $var => $value) {
   $('.php_'. $var).html($value);
   // I don't know phpQuery syntax so I'm using jQuery syntax :)
}
#6

[eluser]Gerson[/eluser]
TheFuzzy0ne, I think that's the idea! Could you give me an exemple of how to write this "front end" file, or do you have an example link?

Colin, you suggestion seems to work, the point is, I'd like to do the phpQuery intervention inside the controller... something like $this->load->view('view', pq('.selected')->text($data));

Btw, I was giving a look on the Template Library and it's pretty much what I was intending to do, but using phpQuery to do not insert php commands inside the pure HTML view file. I'll try to figure out if it's possible to combine phpQuery with Template Library (maybe it's easier).

Thank you guys again.
#7

[eluser]TheFuzzy0ne[/eluser]
Not really, as I'd rather not have to read all of the documentation for something I don't plan on using. I don't know what classes are used by the user, and which are instantiated internally etc...
#8

[eluser]Natebot[/eluser]
Gerson,

Maybe the Inferno library by Elliot Haughin would be of help here.
http://ellislab.com/forums/viewthread/88572/

As I understand it, its job is to act as a wrapper around third party classes so that we can load them using CI_Loader. This saves you the effort of rolling your own "front end" library.

If you want to roll your own - you might look at Elliot's CI libraries on his site which I believe are conversions of classes to CI style libraries.

I think the same strategy that TheFuzzyOne is describing is employed with the Simpletester library that is a CI wrapper for Simpletest. http://codeigniter.com/wiki/SimpleTester...g_library/
Take a look at the simpletester.php library file. It violates the one class per file style rule if that matters to you.
#9

[eluser]Unknown[/eluser]
To use pure HTML as view source and phpQuery as DOM API, best tool will be QueryTemplates. Here's the snippet:
Code:
template()->parse('input.html')->
  find('.my-div')->
    ifVar('showMyDiv')->
    find('ul > li')->
      loopOne('data', 'row')->
        // line below will rapidly populate template with data from $row
        varsToSelector('row', $rowFields)
;
QueryTemplates generates plain vanilla PHP templates which are cached and then included. This approach removes DOM overhead and makes template really fast.

It has other cool features too. Check out project page:
http://code.google.com/p/querytemplates

@Colin Williams: phpQuery syntax is the same as in jQuery, but main function $() is named pq() and methods form $.XXX are in phpQuery static class.
#10

[eluser]Colin Williams[/eluser]
Good stuff, tobiasz. I guess for anyone going this route, I would advise you use it strictly to provide XHTML-looking template tags to be used in the View files. If you used it to, say, set the text of an anchor or change a class here or there, then you're doing too much display logic in the controller.




Theme © iAndrew 2016 - Forum software by © MyBB