css-class-name-template-parser

Gesponsorde koppelingen

PHP script bestanden

  1. css-class-name-template-parser

« Lees de omschrijving en reacties

templateparser.class.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
 * sTemplate class
 *
 * New way to use templates.
 *
 * @author Sebastian Potasiak
 * @version 2.1 BETA
 * @update 28-08-2009
 *
 * @increased at 02-12-2009
 * @increased by Henry van den Berg
 */


class sTemplate
{
   public $dir = "./templates/"; // Templates dir
   public $ext = ".html"; // Template files extension
   public $atr = "class"; // HTML attribute, which value is name of variable


   /**
    * show_error()
    *
    * Showing an error
    *
    * @param message [string] - Error message content
    *
    * @access private
    * @return string
    */

   private function show_error($message)
   {

      echo '<b>Error:</b> ' . $message . '<br /><br />';
      return $message;
   }



   /**
    * parse()
    *
    * Parsing template
    *
    * @param template [string] - Template filename
    * @param values [array] d:0 - Values to replace
    * @param settings [array] d:0 - Settings to replace
    *
    * @access public
    * @return string
    */

   public function parse($template, $values = 0, $settings = 0)
   {

      $path = $this->dir . $template . $this->ext;

      if (!file_exists($path) || !is_readable($path))
      {

         $this->show_error("File does not exists, or is not readable.");
      }


      $file = implode("", file($path));

      if ($values == 0 || !is_array($values)) return $result;

      $result = $this->settings($content, $settings);
      $result = $this->loop($content, $values);

      return $result;
   }



   /**
    * settings()
    *
    * Recursive loop, set the specified objects to its relevant value
    *
    * @param content [string] - Template content
    * @param settings [array] d:0 - Settings to replace
    *
    * @access private
    * @return string
    */

   private function settings($content, $settings = 0)
   {

      if ($settings == 0 || !is_array($settings)) return $content;

      foreach ($settings as $key => $value)
      {

         if (is_array($value))
         {

            foreach ($value as $subkey => $subvalue)
            {

               if (is_array($subvalue))
               {

                  foreach ($subvalue as $subsubkey => $subsubvalue)
                  {

                     $content = str_replace('{' . $key .':'. $subkey .':'. $subsubkey .'}', $subsubvalue, $content);
                  }
               }

               else
               {
                  $content = str_replace('{' . $key .':'. $subkey .'}', $subvalue, $content);
               }
            }
         }

         else
         {
            $content = str_replace('{' . $key . '}', $value, $content);
         }
      }


      return $content;
   }



   /**
    * loop()
    *
    * Recursive loop, which replacing data
    *
    * @param content [string] - Template content
    * @param values [array] d:0 - Values to replace
    *
    * @access private
    * @return string
    */

   private function loop($content, $values = 0)
   {

      if ($values == 0 || !is_array($values)) return $content;

      foreach ($values as $key => $value)
      {

         if (is_array($value))
         {

            preg_match(
                '/\<\!\-\-(\s?)loop ' . $key . ' start(\s?)\-\-\>(.*?)\<\!\-\-(\s?)loop ' . $key . ' end(\s?)\-\-\>/si',
                $content,
                $loop
            );
            $final_value = "";

            foreach ($value as $item)
            {

               $final_value .= $this->loop($loop[3], $item);
            }

                
            $content = preg_replace(
                '/\<\!\-\-(\s?)loop ' . $key . ' start(\s?)\-\-\>(.*?)\<\!\-\-(\s?)loop ' . $key . ' end(\s?)\-\-\>/si',
                $final_value,
                $content
            );
         }

         else
         {
            $content = preg_replace(
                 '/\<([a-zA-Z0-9\"\'\.=\s]+) ' . $this->atr . '=(\'|\"){1}' . $key . '([a-zA-Z0-9\{\}\"\'\.=\s]+)\>(.*?)\<\/([a-zA-Z0-9\"\'\.=\s]+)\>/si',
                 '<\\1 ' . $this->atr . '=\\2' . $key . '\\3>\\4' . $value . '</\\5>',
                  $content
            );
            $content = str_replace('{' . $key . '}', $value, $content);
         }
      }


      return $content;
   }
}

?>


Voorbeeld

index.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
include('./resources/php/templateparser.class.php');

$arrConfig['application']['name'] = "CSS templates";

$arrConfig['module']['version'] = "0.1 BETA";
$arrConfig['module']['language'] = "NL";
$arrConfig['module']['copyright']['year'] = 2009;

$tpl = new sTemplate;

$tpl->dir = './'; // Use only if you have another directory name - this is default
$tpl->ext = '.tpl'; // Like dir - use if you have another
$tpl->atr = 'class'; // Like dir and ext - use if you want to use another attribute in html

// Create an array with variables

$data = array(
    "menu" => array(
        array(
            "item" => "Sinterklaas",
            "current" => "",
            "onclick" => ""
        ),
        array(
            "item" => "Zwarte Piet",
            "current" => "",
            "onclick" => ""
        ),
        array(            
            "item" => "zijn jarig",
            "current" => "current",
            "onclick" => "alert('Hey you!')"
        )
    )
);


// Show parsed template
$tpl->parse("index", $data, $arrConfig);
?>


index.tpl
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<? //voor de kleurtjes
<html xml:lang="{module:language}">

<
head>
   <
title>{application:name}</title>

   <
meta name="copyright" content="Copyright © {module:copyright:year}, all rights reserved" />
   <
meta name="version" content="{module:version}" />

   <
style>
   .
item
   {
      color: #ff0000;
   }

   .
item.current
   {
      color: #00ff00;
   }
   <
/style>
<
/head>

<
body>
   <
div class="menu">
      <!--
loop menu start -->
         <
div class="item {current}" onclick="{onclick}"></div>
      <!--
loop menu end -->
   <
/div>
<
/body>

<
/html>
//voor de kleurtjes ?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.