开发者

PhamlP renders PHP as comments, code isn't executed

开发者 https://www.devze.com 2023-01-31 08:35 出处:网络
So: I quite like using Haml in ruby projects, and was testing it out in PHP. I\'ve been trying PhamlP because it looks like it\'s not dead, whereas phpHaml and pHaml haven\'t been updated in nearly 4

So:

I quite like using Haml in ruby projects, and was testing it out in PHP. I've been trying PhamlP because it looks like it's not dead, whereas phpHaml and pHaml haven't been updated in nearly 4 years.

Now, the problem I have is the php code that PhamlP is parsing is showing up as comments in HTML instead of executing. Here's the test file, index.php:

include_once('haml/HamlParser.php');
$haml = new HamlParser(array('ugly'=>'false'));
$page = $haml->parse('test.haml');
echo $page;

Here's the test.haml file:

!!!
- $foo = 'bar'
%h1 Foo this
.test= $foo

and here's the output I get in the browser when I view source:

<?php
require_once '/Users/Andrew/Sites/eighty-b/_app/haml/HamlHelpers.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php $foo = 'bar'; ?><h1>Foo this</h1><div class="bar">This better fooacross multiple lines butstill in the bar div!</div><div class="test"><?php echo $foo; ?></div>

So, for instance, the line - $foo = 'bar' isn't being executed, it's somehow passing through to the browser as a comment.

How the heck do yo开发者_开发技巧u fix that?


Don't know it, but the obvious workaround would be to replace the echo $page with:

 eval("?".">".$page);    // oh nooooes, eval is evil!!!!!

I'd assume ->parse does just a conversion, and the output is supposed to be stored in a new .php output/template/cache file usually.


The point of it isn't so much to be evaluated - as it's to prepare PHP code, templates and minimize php/html markup, while keeping things as D.R.Y. as possible (that's my take anyways).


I find it easiest to simply have PHamlP render the page to PHP file, and then include the file. This example assumes you have a directory 'cache' that is chmoded to 755:

include_once('haml/HamlParser.php');
$haml = new HamlParser(array('ugly'=>'false'));
$page = $haml->parse('test.haml', 'cache');
require_once 'cache/test.php';

This way all the PHP code executes and you page displays correctly. The test.php file will be generated every time the page is loaded though, so you will want to remove the code to parse the Haml file in a production environment.


Long and barely related answer, but this is what I do. This was inspired by devers answer above

I use the guard ruby gem, and phamlp to automatically convert my .haml files into .php files

My guard file

require 'guard/plugin'
module ::Guard
  class Phamlp < ::Guard::Plugin
    def run_all
    end

    def run_on_changes(paths)
        paths.each do |path|
            puts path
            puts File.dirname path
            system "php hamlconverter.php #{path} #{File.dirname path}"
        end
    end
  end
end

guard :phamlp do
  watch(/^.+(\.haml)$/)
end

and haml converter.php

<?php require_once 'phamlp/haml/HamlParser.php'; 
$haml = new HamlParser(array('style'=>'nested', 'ugly'=>'compressed', 'debug'=>true));
$haml->parse($argv[1], $argv[2]); ?>

If u now run guard it will track your .haml files and if they change, it will convert them to .php files

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号