View previous topic :: View next topic |
Author |
Message |
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Tue Aug 19, 2014 7:07 am Post subject: Recursive {include} losing track of argument variables |
|
|
Undefined index: SUB_CLASS in smarty_internal_templatebase.php(171)
Classes.php (segment):
Code: |
<?php
$template = $smarty->createTemplate("Classes.tpl",["SELECTED_CLASS"=>$classInfoObject]);
//Get the Undefined Index error. It is suppressed by smarty which leads to infinite recursion because SubClasses.tpl keeps calling itself and $PARENT_CLASS never gets set correctly (I think).
$html = $template->fetch();
?>
|
Classes.tpl:
Code: |
<h1>{$SELECTED_CLASS->Get_Title()}</h1>
{include file="SubClasses.tpl" PARENT_CLASS=$SELECTED_CLASS}
|
SubClasses.tpl
Code: |
{foreach $PARENT_CLASS->GetSubclasses() as $SUB_CLASS}
<h2>{$SUB_CLASS->Get_Title()}</h2> <-this line causes no errors. So SUB_CLASS must be set correctly, right?
{include file="Subclasses.tpl" PARENT_CLASS=$SUB_CLASS} <-if I delete this line, I have no errors
{/foreach}
|
I've used {include} like this before with no problems so I'm sure I am just doing something stupid. But I cannot seem to track it down. Any ideas?
(Note: I tried to post minimal code to demonstrate the error. If you want actual code plz ask.) |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 20, 2014 3:47 am Post subject: |
|
|
What does the GetSubclasses() method return if there are no more subclasses? |
|
Back to top |
|
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Wed Aug 20, 2014 7:38 pm Post subject: |
|
|
U.Tews wrote: | What does the GetSubclasses() method return if there are no more subclasses? |
An empty array.
Thx |
|
Back to top |
|
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Wed Aug 20, 2014 8:15 pm Post subject: |
|
|
I have it happening in another spot now.
I think this example is more clear since I have hard-coded some values.
The error is different:
Maximum function nesting level of '500' reached, aborting!
But it seems like the infinite-recursion/nesting issue is based on the same problem.
In my example below I provided two versions of SubCategories.tpl. The first one gives the error. The second one does not.
Category.tpl
Code: |
{include file="SubCategories.tpl" SUB_CATEGORIES=[1,2,3]}
|
This template gives an error:
(notice the IF statement, which is never true)
SubCategories.tpl
Code: |
{foreach $SUB_CATEGORIES as $CATEGORY}
Value: {$CATEGORY}
{if $CATEGORY == "neverthisvalue"}
{include file="SubCategories.tpl" SUB_CATEGORIES=[4,5,6]}
{/if}
{/foreach}
|
This template does not give an error:
SubCategories.tpl
Code: |
{foreach $SUB_CATEGORIES as $CATEGORY}
Value: {$CATEGORY}
{/foreach}
|
If I replace the hard-coded [1,2,3] and [4,5,6] with $CATEGORY->GetChildCategories(), then I get the error: undefined index $CATEGORY.
I even tried to test $CATEGORY ahead of time, and still got the undefined index. Here is the actual code for that attempt:
MarketCategoryOptions.tpl
Code: |
{foreach $SUB_CATEGORIES as $CATEGORY}
{$CATEGORY->Get_Title()}
{if $CATEGORY->GetChildCategories()}
{include file="MarketCategoryOptions.tpl" SUB_CATEGORIES=$CATEGORY->GetChildCategories()}
{/if}
{/foreach}
|
Even with that IF statement, I get: Undefined index $CATEGORY.
GetChildCategories() always returns an array. if there are no more children, it returns a blank array.
Hopefully this adds some insight. |
|
Back to top |
|
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Wed Aug 20, 2014 8:35 pm Post subject: |
|
|
This appears to be a bug caused by {nocache} blocks.
In all of the examples I have shown you, I did not mention that the code is wrapped in {nocache}'s.
When I remove the {nocache}'s, everything works perfectly.
Note: turning off caching with setCaching(Smarty::CACHING_OFF) did not solve the problem. I had to remove the {nocache} tags. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 20, 2014 9:55 pm Post subject: |
|
|
That does explain the problem.
Please show me all your templates including the {nocache} tags.
When you have recursive {include} from within {nocache} sections with parameter passing it can get tricky. |
|
Back to top |
|
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Wed Aug 20, 2014 10:44 pm Post subject: |
|
|
Its a bit complex because this is a large project and I use both template inheritance and composition (includes) and there are custom smarty plugins...
EDIT: it looks like the code is too long for the forums. Is there a better way to send it?
Thanks! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 20, 2014 11:10 pm Post subject: |
|
|
See my private message how to send the code.
But to give you a hint
Classes.tpl: Code: | <h1>{$SELECTED_CLASS->Get_Title()}</h1>
{nocache}
{include file="SubClasses.tpl" PARENT_CLASS=$SELECTED_CLASS cache_lifetime=3600}
{/nocache}
|
The {include} must be in a {nocache} section that the parameter gets passed and assigned when the code gets executed from cache. Normally this would include template "SubClasses.tpl" not as a cached template. The "cache_lifetime=3600" instructs Smarty to cache "SubClasses.tpl" even if it's called from a {nocache} section.
Same here
SubClasses.tpl
Code: |
{nocache}
{foreach $PARENT_CLASS->GetSubclasses() as $SUB_CLASS}
<h2>{$SUB_CLASS->Get_Title()}</h2>
{include file="Subclasses.tpl" PARENT_CLASS=$SUB_CLASS cache_lifetime=3600}
{/foreach}
{/nocache}
|
|
|
Back to top |
|
edelacruz Smarty Rookie
Joined: 10 May 2010 Posts: 25
|
Posted: Thu Aug 21, 2014 5:51 pm Post subject: |
|
|
This looks like a flaw in my implementation. Thanks for all of your guidance! |
|
Back to top |
|
|