Jump to content

Create a script generator


muuucho

Recommended Posts

I am trying to create a dynamic script generator. My idea is to have the following scripts:

 

input.php 

$colour="brown";

$animal ="cat";

 

template.php

echo "<p>This is a $colour \$animal</p>";

 

magic.php (This script is what I need help to write)

takes input.php and template.php and creates and saves a file output.php like this:

 

output.php

echo "<p>This is a brown $animal.</p>;

 

So, when I run... 

index.php

$animal = "cow";

require ('output.php');

 

...I get this in my browser:

This is a brown cow

Link to comment
Share on other sites

This is really just templating. Just about any templating solution will work.

 

Do you really want the input and template files to be valid PHP code? Doesn't seem like a good idea to me. I mean, it's not like you can execute them as they are now. How about simpler stuff like

 

(input.ini)
colour = "brown"
animal = "cat"
(template.php.tpl)
<?php

echo "<p>This is a {{colour}} $animal.</p>";
<?php

$variables = parse_ini_file("input.ini");
$template = file_get_contents("template.php.tpl");

$pairs = array();
foreach ($variables as $name => $value) {
	$pairs["{{" . $name . "}}"] = $value;
}

echo strtr($template, $pairs);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.