Giter Site home page Giter Site logo

jade.php's Introduction

Jade - template compiler for PHP5.3

Jade is a high performance template compiler heavily influenced by Haml and implemented for PHP 5.3.

Features

  • high performance parser
  • great readability
  • contextual error reporting at compile & run time
  • html 5 mode (using the !!! 5 doctype)
  • combine dynamic and static tag classes
  • no tag prefix
  • clear & beautiful HTML output
  • filters
    • :php
    • :cdata
    • :css
    • :javascript
  • you even can write & add own filters throught API
  • TextMate Bundle
  • VIM Plugin

Public API

$dumper = new PHPDumper();
$dumper->registerVisitor('tag', new AutotagsVisitor());
$dumper->registerFilter('javascript', new JavaScriptFilter());
$dumper->registerFilter('cdata', new CDATAFilter());
$dumper->registerFilter('php', new PHPFilter());
$dumper->registerFilter('style', new CSSFilter());

// Initialize parser & Jade
$parser = new Parser(new Lexer());
$jade   = new Jade($parser, $dumper);

// Parse a template (both string & file containers)
echo $jade->render($template);

Syntax

Line Endings

CRLF and CR are converted to LF before parsing.

Indentation

Jade is indentation based, however currently only supports a 2 space indent.

Tags

A tag is simply a leading word:

html

for example is converted to <html></html>

tags can also have ids:

div#container

which would render <div id="container"></div>

how about some classes?

div.user-details

renders <div class="user-details"></div>

multiple classes? and an id? sure:

div#foo.bar.baz

renders <div id="foo" class="bar baz"></div>

div div div sure is annoying, how about:

#foo
.bar

which is syntactic sugar for what we have already been doing, and outputs:

<div id="foo"></div><div class="bar"></div>

jade.php has a feature, called "autotags". It's just snippets for tags. Autotags will expand to basic tags with custom attributes. For example:

input:text

will expand to <input type="text" /> & it's the same as input( type="text" ), but shorter. Another examples:

input:submit( value="Send" )

will become <input type="submit" value="Send" />.

You can even add you own autotags with:

$parser->setAutotag('input:progress', 'input', array('type'=>'text', class=>'progress-bar'));

that will expands to <input type="text" class="progress-bar" />.

It also supports new HTML5 tags (input:email => <input type="email"/>).

Tag Text

Simply place some content after the tag:

p wahoo!

renders <p>wahoo!</p>.

well cool, but how about large bodies of text:

p
  | foo bar baz
  | rawr rawr
  | super cool
  | go Jade go

renders <p>foo bar baz rawr.....</p>

Actually want <?php echo ... ?> for some reason? Use {{}} instead:

p {{$something}}

now we have <p><?php echo $something ?></p>

Nesting

ul
  li one
  li two
  li three

Attributes

Jade currently supports '(' and ')' as attribute delimiters.

a(href='/login', title='View login page') Login

Alternatively we may use the colon to separate pairs:

a(href: '/login', title: 'View login page') Login

Boolean attributes are also supported:

input(type="checkbox", checked)

Boolean attributes with code will only output the attribute when true:

input(type="checkbox", checked: someValue)

Note: Leading / trailing whitespace is ignore for attr pairs.

Doctypes

To add a doctype simply use !!! followed by an optional value:

!!!

Will output the transitional doctype, however:

!!! 5

Will output html 5's doctype. Below are the doctypes defined by default, which can easily be extended:

$doctypes = array(
       '5' => '<!DOCTYPE html>',
       'xml' => '<?xml version="1.0" encoding="utf-8" ?>',
       'default' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
       'transitional' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
       'strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
       'frameset' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
       '1.1' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
       'basic' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
       'mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
   );

Comments

Jade Comments

Jade supports sharp comments (//- COMMENT). So jade block:

//- JADE
- $foo = "<script>";
p
//- ##### COMMENTS ARE SUPPER! ######
  - switch ($foo)
    -case 2
      p.foo= $foo
//-    - case 'strong'
  //-      strong#name= $foo * 2
    -   case 5
      p some text

will be compiled into:

<?php $foo = "<script>"; ?>
<p>
  <?php switch ($foo) ?>
    <?php case 2 ?>
      <p class="foo"><?php echo $foo ?></p>
    <?php break; ?>
    <?php case 5 ?>
      <p>some text</p>
    <?php break; ?>
  <?php endswitch; ?>
</p>

HTML Comments

Jade supports HTML comments (// comment). So block:

peanutbutterjelly
  // This is the peanutbutterjelly element
  | I like sandwiches!

will become:

<peanutbutterjelly>
  <!-- This is the peanutbutterjelly element -->
  I like sandwiches!
</peanutbutterjelly>

As with multiline comments:

//
  p This doesn't render...
  div
    h1 Because it's commented out!

that compile to:

<!--
  <p>This doesn't render...</p>
  <div>
    <h1>Because it's commented out!</h1>
  </div>
-->

IE Conditional Comments

Also, Jade supports IE conditional comments, so:

// [if IE]
  a( href = 'http://www.mozilla.com/en-US/firefox/' )
    h1 Get Firefox

will be parsed to:

<!--[if IE]>
  <a href="http://www.mozilla.com/en-US/firefox/">
    <h1>Get Firefox</h1>
  </a>
<![endif]-->

Filters

Filters are prefixed with :, for example :javascript or :cdata and pass the following block of text to an arbitrary function for processing. View the features at the top of this document for available filters.

body
  :php
    | $data = 40;
    | $data /= 2;
    | echo $data;

Renders:

<body>
  <?php
    $data = 40;
    $data /= 2;
    echo $data;
  ?>
</body>

Code

Buffered / Non-buffered output

Jade currently supports two classifications of executable code. The first is prefixed by -, and is not buffered:

- var $foo = 'bar';

This can be used for conditionals, or iteration:

- foreach ($items as $item):
  p= $item

Due to Jade's buffering techniques the following is valid as well:

- if ($foo):
  ul
    li yay
    li foo
    li worked
- else:
  p hey! didnt work

Second is echoed code, which is used to echo a return value, which is prefixed by =:

- $foo = 'bar'
= $foo
h1= $foo

Which outputs

<?php $foo = 'bar' ?>
<?php echo $foo ?>
<h1><?php echo $foo ?></h1>

Code blocks

Also, Jade has Code Blocks, that supports basic PHP template syntax:

ul
  - while (true):
    li item

Will be rendered to:

<ul>
  <?php while (true): ?>
    <li>item</li>
  <?php endwhile; ?>
</ul>

But don't forget about colons : after instructions start (- if(true) :).

There's bunch of default ones: if, else, elseif, while, for, foreach, switch, case.

jade.php's People

Contributors

everzet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jade.php's Issues

[if IE] tag not opening/closing properly

Jade:

!!!
html
  head
    title= 'title'
    // [if IE]
      script(type="text/javascript")
        window.console = { log: function () {} };

  body!= body

Results in HTML:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>title</title><!-- [if IE]<script type="text/javascript">window.console = { log: function () {} };

    </script>--></head>

Should result in HTML:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>title</title><!-- [if IE]><script type="text/javascript">window.console = { log: function () {} };

    </script><![endif]--></head>

Support for attributes without parenthesis

I haven't looked the code yet, but will ASAP.

In the meanwhile, I let here my idea.
The parenthesis could be replaced by a simple comma, for example, this:

a(href: '/login', title: 'View login page') Login

Could be written like this:

a, href: '/login', title: 'View login page' Login

The rationale is simple: is there a comma? then an attribute is comming

.

.

include / extends

Will the partials and inheritance features be included in this project at some point?

Regex failure in PHP 5.4, 5.5 lead to wrong tags

Hello,

When my server was upgraded to PHP 5.4, 5.5 the following warning was shown.
Warning: preg_match(): Compilation failed: invalid range in character class at offset 8 in Everzet/Jade/Lexer/Lexer.php on line 181

Escaping the dash and changing the regex for '/^(\w[:-\w]*)/' solved the problem for me.

::addChild error

when trying to parse a template I get the following error:

Catchable fatal error: Argument 1 passed to Everzet\Jade\Node\BlockNode::addChild() must be an instance of Everzet\Jade\Node\Node, null given, called in /vagrant/lithium/libraries/li3_jade/libraries/jade.php/src/Everzet/Jade/Parser.php on line 59 and defined in /vagrant/lithium/libraries/li3_jade/libraries/jade.php/src/Everzet/Jade/Node/BlockNode.php on line 25 

Here is the template contents that I am trying to parse:

h2 The Jade template engine Rocks
p(style='white-space:pre;')
    | Jade makes writing HTML EASY and Beautiful
    | If you don't know about it
    a(href="http://jade-lang.com/") Get on it!

[EXPIRED] Last point in paragraph

JADE
p Donate via
a(href='/paypal') Paypal
|.

or

p Donate via
  a(href='/paypal') Paypal
  .

OUTPUT
Donate via Paypal .

EXPECTED
Donate via Paypal.

(Look for a space between word and point)

I can't use multibyte char to attribute

input:submit.btn.primary(value="てすと")
will be
<input class="btn primary" value="てすと")

so i modified Lexer/Lexer.php like following.
it's seems ok, what do you think?

diff --git a/src/Everzet/Jade/Lexer/Lexer.php b/src/Everzet/Jade/Lexer/Lexer.php
index 16c55ab..6be2d1a 100644
--- a/src/Everzet/Jade/Lexer/Lexer.php
+++ b/src/Everzet/Jade/Lexer/Lexer.php
@@ -426,6 +426,23 @@ class Lexer implements LexerInterface
$dbrac = false;

     for ($i = 0, $length = mb_strlen($string); $i < $length; ++$i) {
  •                 $c = mb_substr($string,$i,1);
    
  •        if ('"' === $c) {
    
  •            $dbrac = !$dbrac;
    
  •        } elseif ('\'' === $c) {
    
  •            $sbrac = !$sbrac;
    
  •        }
    
  •        if (!$sbrac && !$dbrac && $begin === $c) {
    
  •            ++$nbegin;
    
  •        } elseif (!$sbrac && !$dbrac && $end === $c) {
    
  •            if (++$nend === $nbegin) {
    
  •                $position = $i;
    
  •                break;
    
  •            }
    
  •        }
    
  •     /*
         if ('"' === $string[$i]) {
             $dbrac = !$dbrac;
         } elseif ('\'' === $string[$i]) {
    
    @@ -440,6 +457,7 @@ class Lexer implements LexerInterface
    break;
    }
    }
  •             */
     }
    

Auto escaping should be default

Hallo,

great library and what I'm currently tried worked well. I'm currently working on a integration of this library for Zend Framework.

What I noticed is that the content isn't escaped by default. HAML does this and I think this should be the default in this library too, since it makes life easier and secure.

Current:
p <script>

Result:
<p><script></p>

Expected:
<p>&lt;script&gt;</p>

Also do this when a variable is used:
p= $foobar

And of cause the programmer should be able to forbid escaping:
p!= <script>
and when the escaping is set to off globally:
p&= $foobar
makes it still escape.

(That syntax follows HAMLs)

HTML attributes with comma in value throws the whole tag off

For example, <meta name="viewport" content="width=device-width, initial-scale=1.0"> is a common practice for mobile-friendly development. When written in jade.php as meta(name="viewport", content="width=device-width, initial-scale=1.0"), it is rendered as <meta name="viewport" content="width=device-width" initial-scale="1.0" />. The Javascript version does not currently have this bug.

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.