Jaxel
Well-known member
I built a wiki-like template system onto my CMS. The way it works is actually pretty simple... If I put the following code into a page, it will replace it with the appropriate pre-defined template:
The way it works, is when it is building the page, it sends the content of the page to a model:
Allow me to try to explain how this code works:
Line 19 is the following code:
However, like I said, I know there is nothing wrong with my code, since the IF does process correctly and it does then go onto ECHO which is within the IF, and the results of the echo are correct.
Code:
[template=charBig]
| name = Cassandra Alexandra
| debut = Soulcalibur II
| voiceE = Debra Jean Rogers
| voiceJ = Reiko Takagi
[/template]
The way it works, is when it is building the page, it sends the content of the page to a model:
Code:
<?php
class EWRporta_Model_Templates extends XenForo_Model
{
public function parseTemplates($text)
{
$templates = $this->fetchAllKeyed("
SELECT template_name AS name, template_content AS content
FROM EWRporta_templates
", 'name');
$codePattern = '/\[template=([\w]+)\](.*?)\[\/template\]/si';
$dataPattern = '/([\w\s]+)=(.+)/';
if (preg_match_all($codePattern, $text, $codeMatches))
{
foreach ($codeMatches AS $key => $codeMatch)
{
if (isset($templates[$codeMatches[1][$key]]))
{
$matches = explode("|", $codeMatches[2][$key]);
$replaceText = $templates[$codeMatches[1][$key]]['content'];
foreach ($matches AS $match)
{
if (preg_match($dataPattern, $match, $dataMatch))
{
$replaceText = str_replace("{{{".trim($dataMatch[1])."}}}", trim($dataMatch[2]), $replaceText);
}
}
echo $replaceText;
$text = str_replace($codeMatches[0][$key], $replaceText, $text);
}
}
}
return $text;
}
}
Allow me to try to explain how this code works:
- parseTemplate recieves original content as $text
- database fetches existing templates and stores them into $templates, keyed by the name
- defines regular expression matching patterns
- if regular expression match collects any results in $codeMatches, continue
- for each result stored in the match, continue as $codeMatch
- if template['name of match'] actually exists, continue, otherwise ignore
- the rest is parsing of the results and replacement text, which I dont need to get into for this discussion. needless to say, I know it all works because "echo $replaceText" actually works and displays the proper results
Server Error
Undefined offset: 2
- XenForo_Application::handlePhpError() in EWRporta/Model/Templates.php at line 19
- EWRporta_Model_Templates->parseTemplates() in EWRporta/ControllerPublic/Index.php at line 104
- EWRporta_ControllerPublic_Index->actionPages() in XenForo/FrontController.php at line 303
- XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
- XenForo_FrontController->run() in /home/eightway/public_xen1/index.php at line 15
Line 19 is the following code:
Code:
if (isset($templates[$codeMatches[1][$key]]));
However, like I said, I know there is nothing wrong with my code, since the IF does process correctly and it does then go onto ECHO which is within the IF, and the results of the echo are correct.