Categories > TinyButStrong general >

Joomla: Question on technique

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Bruce Decker
Date: 2009-06-17
Time: 23:48

Joomla: Question on technique

I'm a Joomla developer and new to TBS.  I've been reading the manuals and examples to get my head around how TBS is dealing with the merging of query results.

Here is my challenge:

I want to retrieve a dozen columns from a table
One of the columns is a blob containing an XML string
I want to convert the XML string retrieved from the blob into a mergeblock
I am using SimpleXML for most of my XML processing.

I would like to:
1) call a mergeblock within a script containing the query that returns the xml blob
2) Pass the xml string returned from the first mergeblock through a simpleXML conversion from string to array
3) Use the array as the source of a mergeblock so that I can use the xml tags from the blob like [xml_mergeblock.xmltag]

I've thought about processing the query myself in my script then setting the TBS-> vars manually but I have this nagging feeling there is a simpler solution.

Anybody care to offer some advice?
By: RwD
Date: 2009-06-18
Time: 08:05

Re: Joomla: Question on technique

I never let tbs handle any of my queries and I always merge in arrays. Because in my opinion a template engine has nothing to do with accessing a database, it should just deal with templates. Also I've made an adapter class to make tbs work with the framwork I've made, this adapter class also changes the tplvars behavior which I always set manualy.

This is the actual beginning of my adapter
class vaTemplateAdapter extends clsTinyButStrong {
  function loadTplVars($newTplVars) {
    if ( is_array($newTplVars) ) {
      foreach( $newTplVars as $key => $value ) {
        $this->TplVars[$key] = $value;
      }
    }
  }
But you need to replace function LoadTemplate as well, so not to reset the tplVars on load. With this change you can set a lot of variables for use inside the templates onload (which is the fastest for as far as I know)

TBS is a pretty damn nice template engine; it is the only one I know that did not reinvent php code and put that into an html template like smarty does (all I see when I look at smarty templates is programming codes structured just as php is, and I do not need smarty for that. (though perhaps for caching)). TBS templates are free of any code. But you may get speed issues if you do not understand how data is merged, and that is a big disadvantage in my opinion; a 36kb template with many of the wrong tags merged with 100 rows from the database took me 22 seconds for the page to load. I've changed the merging order and some of the tags for better timing and now the same page loads (from the internet) in 2 seconds.

By: Bruce Decker
Date: 2009-06-18
Time: 09:18

Re: Joomla: Question on technique

Thanks for the advice.  I believe I am hearing that it may be a better practice to manage the query myself then use mergeblock-array instead of mergeblock-mysql.  This is the direction I was headed to get visibility to the query result.  My confusion came from how TBS fetches the records from the query result.  With that understanding, it would be easier to program my script.  It appears that Mergeblock processes the query (calls the script) once then holds a cursor to the query result and performs row fetches repeating the block until the fetch stack is exhausted.  If this is true, then I just need to understand, perhaps my further examination of examples a bit more about how to feed a mergeblock from arrays as well as queries.  It seems like feeding from arrays is the ticket.

Thanks for the advice.  It's pointing me in the right direction.

Bruce Decker
By: RwD
Date: 2009-06-18
Time: 09:56

Re: Joomla: Question on technique

I am not sure what Skrol29 has to say about it.

But for as far as I know tbs indeed just performs row fetching on the query result untill hitting the end. In fact, if you where to use multpile pages to display a result set tbs itself is extremely inefficient because it tries to support all of these sql databases; no limit clause is added and the entire result set is used on every page. So it is better to handle queries yourself.
By: TomH
Date: 2009-06-18
Time: 12:38

Re: Joomla: Question on technique

Not sure I completely understand what you're trying to do - without an example it's hard for me to understand much of anything ;)

But maybe you could look at the TBS facility to perform event functions. That will allow you to process the xml coming from the database in line with the individual records being processed.

For a simplified example see the 'Event functions' case at http://www.tinybutstrong.com/examples.php

I have a few TBS demonstrations that use more involved functions at http://tomhenry.us/tbs3/ you'll find source code for various 'ondata' functions in the "Menu Tree", "TBS Calendar", and "Cloaking Email Address" examples.

Hope that helps,
TomH
By: Skrol29
Date: 2009-06-18
Time: 12:49

Re: Joomla: Question on technique

Hi,

This is correct. MergeBlocks does open a recordset, read and merge a record ; and so one until the end of the recordset. This is the same for any data source (MySQL, PHP Array, ...).

The ByPage plug-in is using a hook to stop the reading loop when needed, but this is very inefficient as the documentation tells it. Using SQL with a LIMIT clause is a best practice.

In the case of bruce, I think it is better to first save the data in a PHP array, then merge the PHP array. Many XML parser are very slow, and a often prefer to make the conversion myself with a small PHP function.
Another solution is to code a small database plug-in for your XML purpose.



By: Bruce Decker
Date: 2009-06-18
Time: 16:58

Re: Joomla: Question on technique

Tom, Skrol and Rwd,
Thanks so much.  You've allowed me to eliminate some gaps in my thinking.  I feel confident in the direction of my testing and Tom, I'll be sure to review the examples you have identified.

Thanks again guys.  I'm impressed by the tip of the TBS iceberg and I'm sure I'll be even more impressed as I dig further.

As I draw up my own documentation/notes, I'll share it in case it become useful to other newbies.

Bruce Decker