mustache

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/mustache.git
Log | Files | Refs

Mustache.test.php (2073B)


      1 <?php
      2 
      3 use cms\mustache\Mustache;
      4 
      5 require_once('./Mustache.class.php');
      6 
      7 
      8 error_reporting(E_ALL);
      9 ini_set('display_errors', 1);
     10 
     11 $source = <<<SRC
     12 Zahl: {{Zahl}}.{{Zahl}}.{{Zahl}}.
     13 Hello {{planet}}, {{& planet }}.{{! Simple example with a simple property }}
     14 
     15 {{#test}}
     16 Yes, this is a {{test}}. {{! yes, it is}}
     17 {{/test}}
     18 {{^test}}
     19 No, this is not a {{test}}. {{ ! will not be displayed, because test is not false }}
     20 {{/test}}
     21 
     22 {{#car}}
     23 My Car is {{color}}. {{! this is a property of the array car }}
     24 It drives on {{& planet }}.{{! this property is inherited from the upper context }}
     25 {{/}}
     26 
     27 {{#house}}
     28 My House is {{size}}. {{! this property is read from an object }}
     29 {{/}} {{! short closing tags are allowed }}
     30 
     31 Some names:
     32 {{#names}}
     33 my name is {{ name }}.{{! yes, spaces are allowed}}
     34 {{/names}}
     35 
     36 {{#empty}}
     37 this is not displayed {{! because the list is empty }}
     38 {{/empty}}
     39 
     40 {{#upper}}
     41 Hello again, {{planet}}. {{!displayed in uppercase}}
     42 {{/}}
     43 
     44 <h1>Partials</h1>
     45 {{> mycoolpartial}}
     46 
     47 <h1>Changing Delimiters</h1>
     48 Default: {{name}}
     49 {{=$( )=}}
     50 Bash-Style: $(name)
     51 Default should not work here: {{name}}
     52 
     53 $(={{ }}=)
     54 Default again: {{name}}
     55 
     56 <h1>Dot notation</h1>
     57 this will not work: {{building}}
     58 but this is the color of the roof: {{building.roof.color}}
     59 
     60 
     61 SRC;
     62 
     63 $m = new Mustache();
     64 $m->partialLoader = function($name) {
     65  //return '';
     66  return "\n{{Zahl}}{{Zahl}}{{Zahl}} {{Zahl}} {{Zahl}}.This is a partial named ".$name.". It may include variables, like the name '{{name}}'.\n\n";
     67 };
     68 $m->parse( $source );
     69 
     70 echo 'Object: <pre><code>'; print_r($m); echo '</code></pre>';
     71 
     72 $data = array(
     73     'planet'  => '<b>world</b>',
     74     'Zahl'   => '112-000,00',
     75     'test'  => 'Test',
     76     'name'  => 'Mallory',
     77     'car'   => array('color'=>'red'),
     78     'house' => (object) array('size'=>'big' ),
     79     'names' => array(
     80         array('name'=>'Alice'),
     81         array('name'=>'Bob')
     82     ),
     83     'empty' => array(),
     84     'upper' => static function($text) { return strtoupper($text); },
     85     'building' => array('roof'=>array('color'=>'gray'))
     86 
     87 );
     88 
     89 echo '<pre>'.$m->render( $data ).'</pre>';