Git Product home page Git Product logo

phphaml's Introduction

This is a fork of the PHPHaml project. I thought it would be more effective to track patches using github rather than just emailing them to upstream.

So far the patches included here are:

  • "magic else" -- PHPHaml allows templates like the following:

    - if(false)
      Hi
    - else
      Bye

    However, upstream breaks on this "else" because the regex being used to look for this case doesn't match the code that generates it.

  • dashes in id names: fix a bug where ids like #abc-def weren't recognized as XHTML ids.
  • arrays as hashes: allow passing hashes to the element attribute tag, like this:

    - $ary = array('name' => 'myname', 'href' => "#myname")
    %a{ $ary, :rel => 'link' }

    The behavior is not exactly like Ruby HAML, because the attributes specified in the element itself will all come at the end, but it's a start.

  • pipe handling: Ruby HAML treats this as a special case:

    Some text
    |
    More text

    If a pipe is the first character on a line, not counting indentation, it does not count as a line break. Since HTML designers tend to use the pipe character as a separator, it's important to get this case right.

  • commas: Ruby HAML doesn't do much processing on attribute elements, which allows you to do things like this:

    %a{ :href=>"a,b,c", :target => "_blank" }

    PHPHaml has to do some processing here to transform :href into "href" and put everything into an array(). To do this, it splits on commas, which is obviously problematic if one of your values has a comma in it.

    The patch to fix this relies on the similarity between attribute-hash syntax in HAML and an argument array, and simply runs a regex to map :symbol to "symbol". Then we don't have to split on commas in order to split up arguments -- the PHP parser will do it for us.

    This might give you pause, because of the "arrays as hashes of attributes" feature above. But this works, due to a PHP oddity --arrays can have both numeric and non-numeric keys, and the syntax expressly allows you to mix them, as follows:

    $a = array('foo' => 'bar', 'baz');

    ($a['foo'] is 'bar', and $a[0] is 'baz'.) Using this behavior, we can still rely on array literal syntax:

    %a{ :href=>$key, $arguments }

    becomes:

    <a <?php $this->writeAttributes(array('href'=>$key, $value)); ?>>

    And the writeAttributes method is smart enough to recognize that $value has an integer key, so render it recursively.

  • reentrancy: previously PHPHaml had a giant static $aVariables array, which was modified by calling assign() on any HamlParser object. This sucks if you have multiple HamlParsers, want to render HAML recursively, etc. Turning that into an object-local variable was pretty trivial. Additionally, we found it convenient to pass a $context array to render(), which is used in addition to $this->aVariables, to populate the scope of the HAML code.
  • class design: PHPHaml upstream has all HAML processing logic in one huge HamlParser class. It turns out you can decompose this at least a little bit into a HamlLine class, which corresponds roughly to a node in a parse tree, with one line of HAML to compile and some number of children, and a HamlParser that subclasses HamlLine and adds some whole-file code. This is a little easier to work with.
  • whitespace eaters. HAML defines two element modifiers that eat whitespace: %foo> and %foo<. PHPHaml upstream supports neither; we only support the outside-the-element eater (%foo>).

phphaml's People

Contributors

glasserc avatar tyaga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phphaml's Issues

Filters

Filter handling is broken, due to the subclassing of HamlLine to HamlParser.
HamlLine does not implement the method getAsSource().
Changing line 1284 to read

$oHaml = new HamlParser($this->sPath, $this->bCompile, $parent, array('line'=>$iLine, 'file'=>$this->sFile));

however fixes this.
Furthermore, since SassParser implements sass() as a static method the callable must be passed to registerBlock in line 217 as follows:

self::registerBlock(__NAMESPACE__ .'\SassParser::sass', 'sass');

The array(instance, method) only works for non-static methods.
I will submit patches for this (or a pull request) soon.

Ruby string interpolation, double equals (==)

In Haml a double equals interpolates a string the same way that a single equals interpolates ruby code, although the doc itself is quite quiet about that. So this

- @student = 'derDoc'
%p= "Hello #{@student}"
produces the same result as
%p== Hello #{@student}

produces:

<p>Hello derDoc</p>
produces the same result as
<p>Hello derDoc</p>

The same (with php vars) run through phphaml however, produces this:

<?php $student = 'derDoc';?><p<?php $this->writeAttributes(array (), array($student)); ?>><?php echo "Hello #"; ?>
</p>
produces the same result as
<p<?php $this->writeAttributes(array (), array($student)); ?>><?php echo Hello #; ?>
</p>

Code insertion following = (equals)

Code insertation following a '=' does not need a space.

-for ($i = 0; $i < 5; $i++)
  =$i

should work. in PhpHaml however the resulting template is this:

-for ($i = 0; $i < 5; $i++)
  <div><?php echo ; ?>
</div>

Variable interpolation

The title says it all, php code interpolation in plain test does not work.

%p You've pressed #{$_POST['run']} times on the submit button.

should result in

<p>You've pressed <?php echo $_POST['run'];?> times on the submit button.</p>

HTML-style attributes

In Haml HTML-style aatributes work just like attribute hashes.
So this:

%html(xmlns="http://www.w3.org/1999/xhtml")

should result in this

<html xmlns="http://www.w3.org/1999/xhtml"></html>

Instead phphaml outputs this:

<html<?php $this->writeAttributes(array ('class' => 'w3 org')); ?>>
  <?php echo ; ?>

Indentation handling

Haml allows tabs as well as any amount of spaces as long as they are kept consistent.
phphaml only recognizes multiples of 2.

In addition whitespace inside of filters (think :preserve) MUST be preserved and passed to the filter as is.
Even if it is inconsistent. In other words, countLevel() should not throw an error inside of :filter code.

Implement Haml predefined filters

Haml comes predefined with quite a few filters (javascript, css etc).
These should be implemented by default in phphaml.
I already wrote a class for this, will submit it to my fork soon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.