Domanda

I wanted to make a contact form at my nanoc site and I need to put some php code in my .haml file - is there any way (filter maybe?) to easily achieve that or I have to save whole page as a .php (and with stantard html syntax instead of haml one?).

Or maybe there is pure Ruby solution(which can be achieved when hosting on github?)

Please help!

È stato utile?

Soluzione

Haml 3.2 is due to be released soon (rc3 has just been released), and there should be a new haml-contrib gem released along with it. haml-contrib has a PHP filter that might be what you’re looking for.

The doc comment for the php filter says:

# A PHP Filter for Haml. This simply wraps code inside <?php ?> tags. While this
# may seem like a strange idea, some people use Haml to generate mostly static
# HTML documents that then include small amounts of PHP.
#
# This code also serves as an example of how to implement a simple filter for
# Haml.

It doesn’t really do much more than the :plain filter in Kevin Granger’s answer, but might make things slightly easier.

An example:

$ cat php.haml 
:php
  foreach ($es as $e) {
    echo $e;
  }
$ haml -rhaml/filters/php php.haml 
<?php
  foreach ($es as $e) {
    echo $e;
  }
?>

Altri suggerimenti

You can use the :plain filter to preserve the php, it does not parse the filtered text. This is useful when you to keep multiple line of php.

ex:

:plain
  <?php foreach ($es as $e) {
    echo $e;
  } ?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top