Jump to content

How Do I... (Mini Template Engine)


mattd8752

Recommended Posts

I am designing a Mini Template Engine with my site.  Anyway, I've got the code:

<?php
function bbcode($code){
$array=array(
"[b]" => "<b>",  //This sets the value of [b] to <b> so if you looked under [b] in the array you would find <b>.
"[/b]" => "</b>",

"[i]" => "<i>",
"[/i]" => "</i>",

"[u]" => "<u>",
"[/u]" => "</u>",

"[h1]" => "<h1>",
"[/h1]" => "</h1>",
"[img]" => "<img src=\"",
"[/img]" => "\" alt=\"\">",
"[url=\"" => "<a href=\"",
"\"]" => "\">",
"[/url]" => "</a>",
"[div class=\"" => "<div class=\"",
"[/div]" => "</div>",
);

$newtext = str_replace(array_keys($array), array_values($array), $code);
return $newtext;
}

function parse($file){
$content = file($file);
$type = explode("]", $content[0]);
$type = explode("[", $type[0]);
$type = $type[1];



$author = explode(":", $content[1]);
  if($author[1] == "Author"){
  $disp = $author[0];
    if($disp = "yes"){
    $author = $author[2];
    }else{
    $author = "0";
    }
  }else{
  die("Error in the AUTHOR code.  Please check your formatting and ensure that you use our sample template.  If you continue experiencing this issue, please post on our support forums.");
  }



$title = explode(":", $content[2]);
  if($title[0] == "Title"){
  $title = $title[1];
  }else{
  die("Error in the AUTHOR code.  Please check your formatting and ensure that you use our sample template.  If you continue experiencing this issue, please post on our support forums.");
  }

$size = count($content);

$i=3;
$main = "";
  while ($i < $size) {

    if($function != "scripting"){
      if(trim($content[$i]) == "{script}"){
      $function = "scripting";
      $line = 0;
      }else{
      $main .= $content[$i]."<br />\n";
      }

    }else{
    $linec = explode("-", $content[$i]);
      if($linec[0] == "File"){
      $inc = $linec[1];
      $handle = fopen(trim($inc), "rb");
        while (!feof($handle)) {
        $main .= fread($handle, 8192);
        }
      fclose($handle);
      }elseif(trim($content[$i]) == "{/script}"){
      $function = "regular";
      }
    }
  $i++;
  }
$main = bbcode($main);


//$q = "SELECT * FROM themes WHERE type = '".$type."'";
//$q=mysql_query($q);
//$rows=mysql_num_rows($q);
//  if($rows > 1){
//  die("Error, there is more than one template with the name: ".$type.".  Please delete one or select a different template.");
//  }
//$css=mysql_result($result,0,"location");
//$notice=mysql_result($result,0,"notice");
$css = "";
$notice = "";
$output = "<html>\n";
$output .= "<head>\n";
$output .= "<title>".$title."</title>\n";
$output .= "<LINK href=\"".$css."\" rel=\"stylesheet\" type=\"text/css\">\n";
$output .= "</head>\n";
$output .= "<body>\n";
$output .= $main;
$output .= "</body>\n";
$output .= "</html>";
return $output;
}

?>

What I want is to have the <br /> Not appear after DIVs (and other SPECIFIED BBcodes, not all).

 

Maybe if there is a way to check if the line trimmed ends with ].  Any help would be appreciated.  Feel free to take a copy of this script if you like it.

Link to comment
https://forums.phpfreaks.com/topic/39573-how-do-i-mini-template-engine/
Share on other sites

"What I want is to have the

Not appear after DIVs (and other SPECIFIED BBcodes, not all)."

 

It's hard to help because it's not clear what you want.

 

"Maybe if there is a way to check if the line trimmed ends with ]"

 

Check out substr, strrpos, etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.