Created
May 18, 2014 00:56
-
-
Save oaass/85f3508b70209082c8ae to your computer and use it in GitHub Desktop.
Simple text template parser for making configs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('DB_HOSTNAME', '{{hostname}}'); | |
define('DB_USERNAME', '{{username}}'); | |
define('DB_PASSWORD', '{{password}}'); | |
define('DB_DATABASE', '{{database}}'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// DO ALL SORTS OF MAGIC HERE ...... | |
$configTemplate = file_get_contents('config-template.txt'); | |
$data = [ | |
'hostname' => 'localhost', | |
'username' => 'dbuser', | |
'password' => 'dbpass', | |
'database' => 'dbname' | |
]; | |
$configs = parseTemplate($configTemplate, $data); | |
file_put_contents('config.php', $configs); | |
// CONTINUE WITH WHATEVER SORTS OF MAGIC HERE ...... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parseTemplate($template, $data) | |
{ | |
return preg_replace_callback('/{{(\w+)}}/', function($match) use ($data) { | |
return $data[$match[1]]; | |
}, $template); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment