PHP code example of kodhe / parser
1. Go to this page and download the library: Download kodhe/parser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kodhe / parser example snippets
// Basic usage
$parser = new \Kodhe\Framework\Parser\Parser();
// Parse a template with variables
$template = 'Hello, {name}!';
$data = ['name' => 'World'];
echo $parser->parse($template, $data);
// Parse string (alias)
echo $parser->parse_string($template, $data);
// Custom delimiters
$parser->set_delimiters('[', ']');
$template = '{title} - {content}';
$data = ['title' => 'Hello', 'content' => 'World'];
$parser->parse($template, $data);
// Output: Hello - World
$template = '{loop items}<li>{items}</li>{/loop}';
$data = ['items' => ['A', 'B', 'C']];
$parser->parse($template, $data);
// Output: <li>A</li><li>B</li><li>C</li>
$template = '{loop categories}{category}: {loop products}{products}, {/loop}{/loop}';
$data = [
'categories' => [
['category' => 'Fruits', 'products' => ['Apple', 'Banana']],
['category' => 'Vegetables', 'products' => ['Carrot']]
]
];
$template = '{if show}Visible{/if}';
$data = ['show' => 'yes'];
$parser->parse($template, $data);
// Negation
$template = '{if !hidden}Visible{/if}';
$data = ['hidden' => ''];
$parser->setViewPaths(['/path/to/views']);
$template = '{
$parser->set_delimiters('{{', '}}');
$template = '{{greeting}}, {{name}}!';
use Kodhe\Framework\Parser\Factory\ParserFactory;
// Default
$parser = ParserFactory::make();
// With config
$parser = ParserFactory::makeWithConfig([
'left_delimiter' => '{{',
'right_delimiter' => '}}',
'cache_enabled' => false,
'view_paths' => ['/path/to/views']
]);
// With custom components
$parser = ParserFactory::makeWithComponents(
new CustomLexer(),
new CustomCompiler(),
new CustomCache()
);
$parser->setCacheEnabled(false);
$parser->clearCache();
$template = '{user.name} - {user.email}';
$data = ['user' => ['name' => 'John', 'email' => '[email protected] ']];
// Output: John - [email protected]