PHP Classes

MyTemplate Class: Template engine using eval to replace variables

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 653 All time: 4,916 This week: 560Up
Version License PHP version Categories
mytemplateclass 1GNU General Publi...4Templates
Description 

Author

This is a simple template engine class that uses the PHP eval() function to replace variables contained in the template files.

The class and examples code comments are in Arab.

Picture of almubarmij
Name: almubarmij <contact>
Classes: 1 package by
Country: Saudi Arabia Saudi Arabia
Age: 45
All time rank: 293510 in Saudi Arabia Saudi Arabia
Week rank: 360 Up3 in Saudi Arabia Saudi Arabia Up

Example

<?php

// ملف الإعدادات
 
include "inc/config.php";

// ملف الترويسة
 
include "templates/$theme/header.html";

// ضمن المتصفح فاعتبره رقم الصفحة $_GET['page'] إذا تم تعريف
if(isset($_GET['page']))
{
   
$pageNum = $_GET['page'];
}

// حساب السجلات في كل صفحة
$offset = ($pageNum - 1) * $rowsPerPage;

######################################################################
// استخلاص البيانات من القاعدة
$query = "SELECT * FROM `myTable` LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query 1 failed');

// استدعاء دالة القوالب
include("inc/MyTemplate.class");

// تعريف القالب
$myhtml = MyTemplate::MyTemplate("templates/$theme/DataTable.html");

// عرض محتويات سجلات قاعدة البيانات
while($row = mysql_fetch_array($result))
    {
   
//echo "<ooo>" .$row['id']. " - <font color=red>" .$row['name']. "</font></ooo><br>";

   
$id = $row['id'];
   
$name = $row['name'];
   
$email = $row['email'];
   
$comments = $row['comments'];
   
$website = $row['website'];

   
// استبدال المتغيرات الموجودة في القالب بقيمها من قاعدة البيانات
   
eval ("print stripslashes(\"$myhtml\");");
    }
######################################################################

// بـــدء عرض أرقام الصفحات
print '<br>';
print
'<center>';

// إجمالي عدد السجلات في قاعدة البيانات
$query = "SELECT COUNT(id) AS numrows FROM `myTable` ";
$result = mysql_query($query) or die('Error, query 2 failed1');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// إجمالي عدد الصفحات
$maxPage = ceil($numrows/$rowsPerPage);

// إنشاء رابط لكل رقم صفحة
$self = $_SERVER['PHP_SELF'];
$nav = '';
for(
$page = 1; $page <= $maxPage; $page++)
{
       if (
$page == $pageNum)
       {
          
$nav .= " $page "; // عرض رقم الصفحة الحالية بدون رابط
      
}
       else
       {
          
$nav .= " <a href=\"$self?page=$page\" style=\"text-decoration: none\">[$page]</a> ";
       }
}

// إنشاء رابط الصفحة التالية والسابقة
// وإنشاء رابط لذهاب مباشرة للصفحة الأولى أو الأخيرة

if ($pageNum > 1)
{
   
$page = $pageNum - 1;
   
$prev = " <a href=\"$self?page=$page\" style=\"text-decoration: none\">[الصفحة السابقة]</a> ";

   
$first = " <a href=\"$self?page=1\" style=\"text-decoration: none\">[الصفحة الأولى]</a> ";
}
else
{
   
$prev = '&nbsp;'; // نحن في الصفحة الأولى فلا تضع رابط الصفحة السابقة
   
$first = '&nbsp;'; // ولا رابط الصفحة الأولى كذلك
}

if (
$pageNum < $maxPage)
{
   
$page = $pageNum + 1;
   
$next = " <a href=\"$self?page=$page\" style=\"text-decoration: none\">[الصفحة التالية]</a> ";

   
$last = " <a href=\"$self?page=$maxPage\" style=\"text-decoration: none\">[الصفحة الأخيرة]</a> ";
}
else
{
   
$next = '&nbsp;'; // نحن في الصفحة الأخيرة فلا تضع رابط الصفحة التالية
   
$last = '&nbsp;'; // ولا رابط الصفحة الأخيرة كذلك
}

// بداية عرض روابط استعراض الصفحات
print "<div class='num'>";

if (
$DisplayFirst=="نعم") print $first;
if (
$DisplayPrevious=="نعم") print $prev;
if (
$DisplayNumbers=="نعم") print $nav;
if (
$DisplayNext=="نعم") print $next;
if (
$DisplayLast=="نعم") print $last;

if (
$DisplayTotalPages=="نعم") print "<BR><BR>" . $TotalPagesText . "<font color=Green>" . $maxPage . "</font></div>";
print
"</center>";


// إغلاق الاتصال بقاعدة البيانات
// MySQL Server اختياري .. لكنه مستحسن لتوفير موارد
mysql_close($conn);

?>

</body>
</html>


Screenshots  
  • date.png
  • index.jpg
  Files folder image Files  
File Role Description
Accessible without login Plain text file config.php Conf. class config
Accessible without login Plain text file DataTable.html Data tableData template
Accessible without login Plain text file header.html Data header.html
Accessible without login Plain text file index-Example.php Example Example php script
Accessible without login Plain text file my.css Data my css file
Accessible without login Plain text file myDate.html Data myDate template
Accessible without login Plain text file myDate.php Example Hijry date php script
Accessible without login Plain text file myEnv.html Data myEnv.html
Plain text file MyTemplate.class Class MyTemplate Class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:653
This week:0
All time:4,916
This week:560Up
User Comments (1)