File README.md

Last commit: Wed Dec 25 18:46:45 2024 +0100	Jan Dankert	New: README
1 # Mustache template parser 2 3 This is a simple Mustache template implementation, 4 See https://mustache.github.io/ for the specification. 5 6 This implementation has the following advantages: 7 - no temporary files 8 - no require calls or includes 9 - no eval calls 10 - no weird ob_start calls 11 12 13 But this implementation is probably slower than "Bog the cow's" implementation: 14 https://github.com/bobthecow/mustache.php 15 16 But for some situations (i.e. for statifying CMS) this is not a problem ;) 17 18 ## Features: 19 - Simple values 20 - Comments 21 - Blocks (normal and negating blocks) 22 - Lists with arrays and objects 23 - Wrapper functions 24 - Partials (you need to define a partial loader) 25 - Delimiter change 26 - no dot notation on property names 27 28 29 ## Examples 30 31 ### Template 32 33 Hello {{planet}}, {{& planet }}.{{! Simple example with a simple property }} 34 35 {{#test}} 36 Yes, this is a {{test}}. {{! yes, it is}} 37 {{/test}} 38 {{^test}} 39 No, this is not a {{test}}. {{ ! will not be displayed, because test is not false }} 40 {{/test}} 41 42 {{#car}} 43 My Car is {{color}}. {{! this is a property of the array car }} 44 It drives on {{& planet }}.{{! this property is inherited from the upper context }} 45 {{/}} 46 47 {{#house}} 48 My House is {{size}}. {{! this property is read from an object }} 49 {{/}} {{! short closing tags are allowed }} 50 51 Some names: 52 {{#names}} 53 my name is {{ name }}.{{! yes, spaces are allowed}} 54 {{/names}} 55 56 {{#empty}} 57 this is not displayed {{! because the list is empty }} 58 {{/empty}} 59 60 {{#upper}} 61 Hello again, {{planet}}. {{!displayed in uppercase}} 62 {{/}} 63 64 <h1>Partials</h1> 65 {{> mycoolpartial}} 66 67 <h1>Changing Delimiters</h1> 68 Default: {{name}} 69 {{=$( )=}} 70 Bash-Style: $(name) 71 Default should not work here: {{name}} 72 73 $(={{ }}=) 74 Default again: {{name}} 75 76 <h1>Dot notation</h1> 77 this will not work: {{building}} 78 but this is the color of the roof: {{building.roof.color}} 79 80 81 ## Using 82 83 $m = new Mustache(); 84 $m->partialLoader = function($name) { 85 return "\nThis is a partial named ".$name.". It may include variables, like the name '{{name}}'.\n\n"; 86 }; 87 $m->parse( $source ); 88 89 echo 'Object: <pre><code>'; print_r($m); echo '</code></pre>'; 90 91 $data = array( 92 'planet' => '<b>world</b>', 93 'test' => 'Test', 94 'name' => 'Mallory', 95 'car' => array('color'=>'red'), 96 'house' => (object) array('size'=>'big' ), 97 'names' => array( 98 array('name'=>'Alice'), 99 array('name'=>'Bob') 100 ), 101 'empty' => array(), 102 'upper' => static function($text) { return strtoupper($text); }, 103 'building' => array('roof'=>array('color'=>'gray')) 104 105 ); 106 107 echo '<pre>'.$m->render( $data ).'</pre>';
Download README.md
History Wed, 25 Dec 2024 18:46:45 +0100 Jan Dankert New: README