Hey All,
I've spent all morning tring to figure out a problem.First,the scenario is I have a template system which I use to parse all templates for my scripts to display.Now thats all well and good,but for the 'main' template I pass an array containing references to the defined classes of my script.Thats also all well and good,with the exception of one part...I store all the templates in one file,for reference this is an example of a template file including the template in question:
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
now the problem is when I call a template to display the output from a class function(in this case,the blocks) like so:
$moocms["Blocks"]->ShowBlocks();
echo $moocms["Blocks"]->template->fetch();
I get an invalid argument supplied for foreach() on the foreach line of the template.ShowBlocks() is as follows:
function ShowBlocks()
{
$this->db->exec("SELECT name,content FROM neko_blocks WHERE enabled = 1 ORDER BY weight") or $this->RetError($this->db->display_error());
$this->template =& new Template($GLOBALS["MooCMS"]["config"]["t_url"].'/show.php?load=blocks');
$this->template->set('blocks',$this->db->get_arr_assoc());
return true;
}
pretty easy to follow,but basically it queries a db and sets the result set array as the 'blocks' paramater for the template.The following is the code for the template class:
class Template
{
var $vars;
function Template($file = null)
{
$this->file = $file;
}
function set($name,$val)
{
$this->vars[$name] = is_object($val) ? $val->fetch() : $val;
}
function fetch($file = null)
{
if(!$file)
{
$file = $this->file;
}
if(is_array($this->vars))
{
extract($this->vars);
}
ob_start();
include_once($file);
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
I've tried everything I could think of to get it to work,I've been fighting with it since aprox. 7am my time...it's now 6:30pm....so I'm at my wits end and would owe a large favor to anyone who could help me figure this one out.
TIA....the only thing that comes to mind is that it doesn't include the $file like you want. also I don't see where $blocks is being set?Originally posted by scoutt
the only thing that comes to mind is that it doesn't include the $file like you want. also I don't see where $blocks is being set?
$blocks gets set in the call to fetch(); on $this->template in the ShowBlocks(); function...it is calling the file properly...that I'm sure of...as I am able to put other things in the template(such as content other then the loop) and they display fine...but the foreach doesn't work...and $blocks returns NULL when checked with gettype...it confuses me extremely...theres no reason a call to a second instance of the Template class inside of the first template would cause it not to properly parse is there?out of this code
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
I see no place $block gets set. the array has to be set before it is called, right?
if it is in the showblocks function then all that doe it return true, but it also sets
$this->template
as the array you want, that is no where close to $blocks.
am I understanding it correctly?
so maybe you want
foreach($moocms["Blocks"] as $b)
but I don't see where you set it.Originally posted by scoutt
out of this code
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
I see no place $block gets set. the array has to be set before it is called, right?
if it is in the showblocks function then all that doe it return true, but it also sets
$this->template
as the array you want, that is no where close to $blocks.
am I understanding it correctly?
so maybe you want
foreach($moocms["Blocks"] as $b)
but I don't see where you set it.
the switch is part of a template file...which is loaded by the Template class...I probably shouldeve commented my code:
function ShowBlocks()
{
$this->db->exec("SELECT name,content FROM neko_blocks WHERE enabled = 1 ORDER BY weight") or $this->RetError($this->db->display_error()); //query db using referenced db connection
$this->template =& new Template($GLOBALS["MooCMS"]["config"]["t_url"].'/show.php?load=blocks'); //create instance of Template class loading the template for use
$this->template->set('blocks',$this->db->get_arr_assoc()); //sets the template variable $blocks to the value of the array and adds it to the $this->template->vars array
return true;
}
so you see...what I'm actually doing is just setting a value for later use...the atchual variable isn't set until the call to fetch(); which is:
$moocms["Blocks"]->ShowBlocks(); //execute above ShowBlocks function
echo $moocms["Blocks"]->template->fetch(); //extract variables from $moocms["Blocks"]->template->vars array to local namespace for template,and output result
basically the call to $this->template->set('blocks',$this->db->get_arr_assoc());
sets the value of $blocks to $this->db->get_arr_assoc(); ...after the fetch(); method is called.So..in a way...$blocks is set by the fetch(); method...which sets it as a variable accessable by the template file.show the code in it's entirity. I still don't see where $blocks = the array.[edit: sorted]
Thanks Anyway Scoutt...I figured out it was my fault...using $_GET["load"] in the switch was my problem.So I just set it as a template var instead what template system are you using?Originally posted by scoutt
what template system are you using?
Code is posted above....class Template ^^...I don't use premade scripts.Regardless of what they are...heh.oh you wrote it yourself? I thought it was a package. Originally posted by scoutt
oh you wrote it yourself? I thought it was a package.
yeah...wrote it myself,standard method of using 'php as the template language' ...sorry if the variable names or sommat confused you...little covert op on my part ..suffice to say...Moo ish mine!Originally posted by willamoose
...little covert op on my part ..suffice to say...Moo ish mine!
well it worked
I need to do one of those but it will have to wait until later.Originally posted by scoutt
well it worked
I need to do one of those but it will have to wait until later.
I knew you'd understand ..however,I can honestly say my covert op will be dwarved by yours.However,should you wish to be horrified,the battle plans are available to those who are among my few wartime advisors(thus being inclusive of you).However,this covert op is the predecessor of a full scale military assult which is being formed piece by piece.Meetings with my advisors are held at various times in various venues.
I've spent all morning tring to figure out a problem.First,the scenario is I have a template system which I use to parse all templates for my scripts to display.Now thats all well and good,but for the 'main' template I pass an array containing references to the defined classes of my script.Thats also all well and good,with the exception of one part...I store all the templates in one file,for reference this is an example of a template file including the template in question:
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
now the problem is when I call a template to display the output from a class function(in this case,the blocks) like so:
$moocms["Blocks"]->ShowBlocks();
echo $moocms["Blocks"]->template->fetch();
I get an invalid argument supplied for foreach() on the foreach line of the template.ShowBlocks() is as follows:
function ShowBlocks()
{
$this->db->exec("SELECT name,content FROM neko_blocks WHERE enabled = 1 ORDER BY weight") or $this->RetError($this->db->display_error());
$this->template =& new Template($GLOBALS["MooCMS"]["config"]["t_url"].'/show.php?load=blocks');
$this->template->set('blocks',$this->db->get_arr_assoc());
return true;
}
pretty easy to follow,but basically it queries a db and sets the result set array as the 'blocks' paramater for the template.The following is the code for the template class:
class Template
{
var $vars;
function Template($file = null)
{
$this->file = $file;
}
function set($name,$val)
{
$this->vars[$name] = is_object($val) ? $val->fetch() : $val;
}
function fetch($file = null)
{
if(!$file)
{
$file = $this->file;
}
if(is_array($this->vars))
{
extract($this->vars);
}
ob_start();
include_once($file);
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
I've tried everything I could think of to get it to work,I've been fighting with it since aprox. 7am my time...it's now 6:30pm....so I'm at my wits end and would owe a large favor to anyone who could help me figure this one out.
TIA....the only thing that comes to mind is that it doesn't include the $file like you want. also I don't see where $blocks is being set?Originally posted by scoutt
the only thing that comes to mind is that it doesn't include the $file like you want. also I don't see where $blocks is being set?
$blocks gets set in the call to fetch(); on $this->template in the ShowBlocks(); function...it is calling the file properly...that I'm sure of...as I am able to put other things in the template(such as content other then the loop) and they display fine...but the foreach doesn't work...and $blocks returns NULL when checked with gettype...it confuses me extremely...theres no reason a call to a second instance of the Template class inside of the first template would cause it not to properly parse is there?out of this code
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
I see no place $block gets set. the array has to be set before it is called, right?
if it is in the showblocks function then all that doe it return true, but it also sets
$this->template
as the array you want, that is no where close to $blocks.
am I understanding it correctly?
so maybe you want
foreach($moocms["Blocks"] as $b)
but I don't see where you set it.Originally posted by scoutt
out of this code
<?switch($_GET["load"]):?>
/*removed for clarity*/
<?case 'blocks':?>
<b>Blocks</b><br/>
<?foreach($blocks as $b):?>
<u><?=$b["name"]?></u><br/>
<?=$b["contents"]?><br/>
<?endforeach;?>
<?break;?>
<?endswitch;?>
I see no place $block gets set. the array has to be set before it is called, right?
if it is in the showblocks function then all that doe it return true, but it also sets
$this->template
as the array you want, that is no where close to $blocks.
am I understanding it correctly?
so maybe you want
foreach($moocms["Blocks"] as $b)
but I don't see where you set it.
the switch is part of a template file...which is loaded by the Template class...I probably shouldeve commented my code:
function ShowBlocks()
{
$this->db->exec("SELECT name,content FROM neko_blocks WHERE enabled = 1 ORDER BY weight") or $this->RetError($this->db->display_error()); //query db using referenced db connection
$this->template =& new Template($GLOBALS["MooCMS"]["config"]["t_url"].'/show.php?load=blocks'); //create instance of Template class loading the template for use
$this->template->set('blocks',$this->db->get_arr_assoc()); //sets the template variable $blocks to the value of the array and adds it to the $this->template->vars array
return true;
}
so you see...what I'm actually doing is just setting a value for later use...the atchual variable isn't set until the call to fetch(); which is:
$moocms["Blocks"]->ShowBlocks(); //execute above ShowBlocks function
echo $moocms["Blocks"]->template->fetch(); //extract variables from $moocms["Blocks"]->template->vars array to local namespace for template,and output result
basically the call to $this->template->set('blocks',$this->db->get_arr_assoc());
sets the value of $blocks to $this->db->get_arr_assoc(); ...after the fetch(); method is called.So..in a way...$blocks is set by the fetch(); method...which sets it as a variable accessable by the template file.show the code in it's entirity. I still don't see where $blocks = the array.[edit: sorted]
Thanks Anyway Scoutt...I figured out it was my fault...using $_GET["load"] in the switch was my problem.So I just set it as a template var instead what template system are you using?Originally posted by scoutt
what template system are you using?
Code is posted above....class Template ^^...I don't use premade scripts.Regardless of what they are...heh.oh you wrote it yourself? I thought it was a package. Originally posted by scoutt
oh you wrote it yourself? I thought it was a package.
yeah...wrote it myself,standard method of using 'php as the template language' ...sorry if the variable names or sommat confused you...little covert op on my part ..suffice to say...Moo ish mine!Originally posted by willamoose
...little covert op on my part ..suffice to say...Moo ish mine!
well it worked
I need to do one of those but it will have to wait until later.Originally posted by scoutt
well it worked
I need to do one of those but it will have to wait until later.
I knew you'd understand ..however,I can honestly say my covert op will be dwarved by yours.However,should you wish to be horrified,the battle plans are available to those who are among my few wartime advisors(thus being inclusive of you).However,this covert op is the predecessor of a full scale military assult which is being formed piece by piece.Meetings with my advisors are held at various times in various venues.