getDocContent limitations on passing snippets
Started by Cris D., Feb 16 2008 12:00 PM
10 replies to this topic
#1
Posted 16 February 2008 - 12:00 PM
I have created a snippet "mini shopping cart" which uses a snippet "cart_functions" (which defines all functions used in the snippet-obviously) that is called into the 'mini shopping cart' with runSnippet. I'm trying to separate it so it can be called throughout the site in a variety of ways.
Calling it directly into a document works OK.
Calling it into a document using runSnippet works OK.
However...
1) Placing it in the template means it it one "refresh" behind the actual order stored in the table.
2) Calling it from a separate document using getDocContent causes a 'call to undefined function' error.
Snippets are uncached (required for updated order display).
Is this a know limitation of the getDocContent snippet? or templates? (I can't see why it should as the snippet parses (5) in the index.php file should be ample for this (3) level execution).
Calling it directly into a document works OK.
Calling it into a document using runSnippet works OK.
However...
1) Placing it in the template means it it one "refresh" behind the actual order stored in the table.
2) Calling it from a separate document using getDocContent causes a 'call to undefined function' error.
Snippets are uncached (required for updated order display).
Is this a know limitation of the getDocContent snippet? or templates? (I can't see why it should as the snippet parses (5) in the index.php file should be ample for this (3) level execution).
#2
Posted 16 February 2008 - 03:46 PM
Thinking back, I have used snippet code in templates, where I have stored functions for use by other snippets... But I think you are correct on the one refresh delay... This is most likely due to the fact that the template is one of the last items called and parsed by the parser... The document has already been fully parsed before it is aded into the template code as I recall... There are probably a few alternative methods that might work better... I'd need to know what each piece of the puzzle does in order to recommend the best option...
#3
Posted 16 February 2008 - 09:16 PM
Quote
But I think you are correct on the one refresh delay..
Quote
I'd need to know what each piece of the puzzle does in order to recommend the best option...
I have a snippet 'products' which when the 'add to cart' button is clicked, a href sends the product id off to the table 'cart' page where a snippet uses switch to $_GET['action'] and case 'add' runs the addToCart() function. The addToCart() function is called into the 'cart' snippet using another snippet 'include_functions' using runSnippet. All OK so far.
I have a second snippet 'miniCart' that uses the same 'include_functions' snippet and updates correctly when called in a single document. The getCartItems() function does this (psudo code):
function getCartItems(){
global $etomite;
$sql='SELECT.....;';
$row=$etomite->dbQuery($sql);
while($result=mysql_fetch_assoc($row)){
$cartContent[]=$row;
}
return $cartContent;
}
The problem, if I add an item to the shopping cart, the change is not reflected in the miniCart immediately if the [!miniCart!] snippet is in the template. I have then been looking at alternate ways to make the snippet available across the site using getDocContent which as I mentioned seems to load the snippet calling the functions before the functions themselves (which I don't think loading into the template will fix if it is really the last thing to be parsed, the function definitions still won't exist). Ideally I'm really after a way to get the snippet in the template to display current table info. If it is not feasible, I will just have to start adding [!miniCart!] to each page in the site...
[EDITED] to fix getCartItems function[/EDIT]
Edited by Cris D., 16 February 2008 - 09:20 PM.
#4
Posted 16 February 2008 - 11:08 PM
PS I've also tried getSideBar, but when called from the template, it returns the unparsed snippet call: [!miniCart!].
#5
Posted 16 February 2008 - 11:57 PM
I'd need to play with things a bit... But here's a possible solution... Why not use XMLHttpRequest()... I have used it for several sites when no other solution worked well enough... Just another possible option... I have some code on my http://ralphdahlgren.com site that makes use of it and all of the code should be available there if you want to give it a shot...
My concept for a cart was to keep the entire cart in a session array, thus eliminating the problems you have described... Nothing would get stored in a database record until the cart is ready to be processed...
My concept for a cart was to keep the entire cart in a session array, thus eliminating the problems you have described... Nothing would get stored in a database record until the cart is ready to be processed...
#6
Posted 17 February 2008 - 07:35 AM
I've grabbed your function createXMLHttpRequest() and will have a play and re-visit the shoutbox module to work out how to integrate it with the etomite getIntTableRows (and other) etomite APIs.
As an aside...
Have you ever considered or successfully written a "snippet-booster" API? Something that would multi-parse a page if it contained un-evaluated snippets? I've tried several attempts without success but it could be a very useful tool if it worked.
Perhaps place at the top of a document or in a template where there are problems parsing the snippet levels and the API will tell the parser to stay and play
Something like:
[!snippetBooster!] in the top of the offending page or in the template, although it produces no additional output, it just flags the page being viewed as a problem child for the parser to do additional work on.
function $snippetBooster($id){
global $etomite;
$id=$etomite->documentIdentifier;
$uncachedMatch=preg_match('[!(^*)!]', $documentContent);
while($uncachedMatch==true)
{
$parsed=$etomite->runSnippet($name,$params);
$documentContent=str_replace('[! (^*) !]', $parsed);
}
return $documentContent;
}
although not actual code, you probably get the idea.
It progressively identify uncached snippets n levels deep within those snippets listed or until all uncached snippets have been parsed. Most of the problems I have found while trying to develop something like this occur with the way etomite is hesitant to parse the snippets without adding other etomite functions into the soup, or returning binary.
Just a thought....perhaps it is more complex as I think it actually is.
As an aside...
Have you ever considered or successfully written a "snippet-booster" API? Something that would multi-parse a page if it contained un-evaluated snippets? I've tried several attempts without success but it could be a very useful tool if it worked.
Perhaps place at the top of a document or in a template where there are problems parsing the snippet levels and the API will tell the parser to stay and play
Something like:
[!snippetBooster!] in the top of the offending page or in the template, although it produces no additional output, it just flags the page being viewed as a problem child for the parser to do additional work on.
function $snippetBooster($id){
global $etomite;
$id=$etomite->documentIdentifier;
$uncachedMatch=preg_match('[!(^*)!]', $documentContent);
while($uncachedMatch==true)
{
$parsed=$etomite->runSnippet($name,$params);
$documentContent=str_replace('[! (^*) !]', $parsed);
}
return $documentContent;
}
although not actual code, you probably get the idea.
It progressively identify uncached snippets n levels deep within those snippets listed or until all uncached snippets have been parsed. Most of the problems I have found while trying to develop something like this occur with the way etomite is hesitant to parse the snippets without adding other etomite functions into the soup, or returning binary.
Just a thought....perhaps it is more complex as I think it actually is.
#7
Posted 17 February 2008 - 10:04 AM
A solution!
By playing with the combination of cached and uncached snippets, I am able to get the miniCart snippet to display current data as follows:
1) Call the snippet [[cached]] in a separate document: published, show in menu, (document cached or uncached makes no difference). 2) In the template, call [!getDocContent?id=72!] the id where the document holding the dynamic snippet.
The effect is to dynamically create the document content from the template level where the cached snippet is evaluated with current data!
By playing with the combination of cached and uncached snippets, I am able to get the miniCart snippet to display current data as follows:
1) Call the snippet [[cached]] in a separate document: published, show in menu, (document cached or uncached makes no difference). 2) In the template, call [!getDocContent?id=72!] the id where the document holding the dynamic snippet.
The effect is to dynamically create the document content from the template level where the cached snippet is evaluated with current data!
#8
Posted 17 February 2008 - 03:27 PM
The Etomite parser is a multi-pass parser already... If you have problems with excessive nesting you can increase the number of passes at the bottom of your index.php file... The default is 5 passes...
As far as caching is concerned, I only cache documents that have nothing but static text for content...
As far as caching is concerned, I only cache documents that have nothing but static text for content...
#9
Posted 17 February 2008 - 03:34 PM
But wouldn't it be cool if the parser new automatically when to increase the number of parses for an individual document depending on the use of excessive nesting...
#10
Posted 17 February 2008 - 03:41 PM
Surely you'd then get loops? Where the parser didn't know when to stop.
Remember, I'm not a programmer.
Remember, I'm not a programmer.
#11
Posted 17 February 2008 - 04:06 PM
An idea that I have, which I haven't tried yet, is to use stack technology for controlling parser passes... The stack would branch off during processing similar to the document tree heirarchy so that parsing takes place, recursively, in the correct order... No preset number of passes would be required as it would all be done auto-magically...
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











