Project Description
PHP View Engine allows you to write ASP.NET MVC views in the PHP language.

Do you have designers, junior web devs or PHP hard-core devs that only know PHP? Now they can particiapte on ASP.NET MVC projects.

Finally, you can have Seperation of Concerns and spaghetti code too!

Take a C# controller like this:

	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			ViewData["message"] = "Powered by ASP.NET MVC!";
			ViewData["a"] = 3;
			ViewData["b"] = 4.7;
			ViewData["numbers"] = new List<string>() { "One", "Two", "Three", "Four" };
			
			var dict = new Dictionary<string, int>();
			dict.Add("Wendy", 45);
			dict.Add("Paul", 38);
			dict.Add("James", 50);
			
			// Return the dictionary as the model
			return View (dict);
		}
		
		// We are getting a "strongly typed" result from the PHP form
		public ActionResult Display(string freetext)
		{
			// We can pass CLR objects back to PHP
			var person = new { Name = freetext };
			
			return View(person);
		}
	}

and a PHP view like this (complete with pattern violating logic for your demo pleasure):

<!DOCTYPE html>
<html>
<head><title>PHP View Engine Demo</title></head>
<body>
<p>Stan says hello from static HTML.<p>
<?
	$arr = array(1 => "Dave", 2 => "Penny", 3 => "Tim");
?>
<p>
<? echo $arr[2]." says hello from PHP!"; ?>
</p>
<? echo $viewdata["message"]; ?>
<hr />
<p>Do question marks screw things up?</p>
<p>
<?
	$a = $viewdata['a'];
	$b = $viewdata['b'];
	
	echo "$a + $b = ".adder($a,$b);
?>
</p>
<ol>
<?
foreach ($viewdata['numbers'] as $number)
{
	echo "<li>$number</li>";
} ?>
</ol>
<ul>
<?
// I love the PHP foreach syntax here
foreach ($model as $name => $age)
{
	echo "<li>$name is $age</li>";
} ?>
</ul>
<table>
<?
// Routing information is available to PHP
foreach($route as $key => $value)
{
	echo "<tr><td>$key</td><td>$value</td></tr>";
} ?>
</table>
<hr />
Enter a person's name: 
<? $url = '/'.$route['controller'].'/Display'; ?>
<form action='<? echo $url; ?>' enctype='multipart/form-data' method=post>
<input type='text' name='freetext' />
<input type='submit' value='Submit' />
</form>
<?
/* I put the function way down here
 * to demonstrate that it does not
 * have to come before first use.
 */
function adder ($x, $y) { return $x+$y; }
?>
</body>
</html>

and voila! You get HTML (HTML5 even!) like this:

<!DOCTYPE html>
<html>
<head><title>PHP View Engine Demo</title></head>
<body>
<p>Stan says hello from static HTML.<p>
<p>
Penny says hello from PHP!
</p>
Powered by ASP.NET MVC!
<hr />
<p>Do question marks screw things up?</p>
<p>
3 + 4.7 = 7.7
</p>
<ol>
<li>One</li><li>Two</li><li>Three</li><li>Four</li>
</ol>
<ul>
<li>Wendy is 45</li><li>Paul is 38</li><li>James is 50</li>
</ul>
<table>
<tr><td>controller</td><td>Home</td></tr>
<tr><td>action</td><td>Index</td></tr>
<tr><td>id</td><td></td></tr>
</table>
<hr />

Enter a person's name: 
<form action='/Home/Display' enctype='multipart/form-data' method=post>
<input type='text' name='freetext' />
<input type='submit' value='Submit' />
</form>

</body>
</html>


The current implementation has serious flaws (but hey, I have to get back to work).

This project relies on the Phalanger PHP compiler for .NET and Mono. I include the Phalanger assemblies for convenience.

Last edited Jul 26, 2011 at 10:04 PM by jmalcolm, version 11