Reputation: 684
So i made a basic template system for a project my company is developing. Currently I am able to load a template and print it out, and use a tag we developed to call templates within another template by calling <template::>template_name<::/template>
in the master template. This works awesome as long as I only call ONE template. After the first occurance of the template tag, the system stops and doesn't go on to read the rest of the string from the database to get possible other template tags.
If i call the SAME template multiple times in the script, it will render right, but if I call a NEW template, it gets ignored by the system and gets printed as a normal text.
Here is my current functions for calling the tags and replacing them:
$inject_template = $this->get_template_name_between($template, "<template::>", "<::/template>");
if(!empty($inject_template)) {
$query_for_template = "SELECT * FROM ".$field." WHERE templateid = '".$inject_template."' LIMIT 1";
$injected_template_data = $wms->function->query($query_for_template);
while($returned = mysql_fetch_array($injected_template_data)) {
$type = $returned['templatetype'];
}
$template = str_replace('<template::>'.$inject_template.'<::/template>', $this->injectTemplate($inject_template, $type), $template);
}
public function injectTemplate($template_id, $template_number_type) {
return $this->extract_template($template_id, $template_number_type);
}
public function get_template_name_between($string, $start, $end) {
$ini = strpos($string, $start);
if($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
Remember, this works if I call ONE tag, I simply need a way to force the function to replace ALL the tags with the right template for each. I was thinking maybe a for loop, and counting, but not sure the correct syntax to do that and I've been staring at this all day now lol. Thanks in advance!
Upvotes: 0
Views: 40
Reputation: 781761
You can use a loop that keeps replacing until there are no templates in the string.
while ($inject_template = $this->get_template_name_between($template, "<template::>", "<::/template>")) {
$query_for_template = "SELECT * FROM ".$field." WHERE templateid = '".$inject_template."' LIMIT 1";
$injected_template_data = $wms->function->query($query_for_template);
$returned = mysql_fetch_array($injected_template_data);
$type = $returned ? $returned['templatetype'] : '';
$template = str_replace('<template::>'.$inject_template.'<::/template>', $this->injectTemplate($inject_template, $type), $template);
}
You don't need to use a while()
loop around mysql_fetch_array()
, since the query is limited to 1 row.
BTW, you should stop using the mysql_*
functions. Convert to mysqli
or PDO
, and use prepared queries.
Upvotes: 1