该软件包已被废弃,不再维护。没有建议的替代软件包。
该软件包的最新版本(v0.3.1)没有可用的许可信息。

PHP最简单的模板引擎

v0.3.1 2013-04-26 07:54 UTC

This package is auto-updated.

Last update: 2020-01-29 20:55:50 UTC


README

Neddle受到了HAML/Jade和CoffeeScript的启发,并不是一个完整的解析器,但它处理模板非常出色。

好吧,它可能存在问题,不会对语法错误抛出任何警告。

基础

与其他类似HAML的模板引擎一样,它使用空白来嵌套你的块。同时也提供了对内联表达式、闭包和几乎所有PHP代码的最小支持。

内联注释

/ html
- # php
; invisible

块注释

/
  large and
  nested html comments

HTML块

#id.class { attr => $value }
  a { href => '#' } link text

p
  nested text
  here

pre
  |and the pipe
  |    preserves the white-space
  | in the block

<table>
  <tbody>
    <tr>
      <td>also you can use raw html</td>
    </tr>
  </tbody>
</table>

<?php echo 'and raw php code'; ?>

\a string value (not parsed)
a string value (parsed)

:some-filter
  any block of text (not parsed)

PHP块

- if $condition
  = 'success'

ul
  - for $i = 0; $i < 10; $i += 1
    li = "Item $i"

- $all = array(1, 2, 3)
p #{$one} @while $one = array_shift($all)

- unless false
  some text

- print_r(call_user_func(~>
  - return range(0, 10)

- $lambda = ($value) ~>
  - return $value

= $lambda('some value')
~ $lambda('<p>escaped text</p>')

正如你所见,语法非常简单且与PHP兼容。:-)

使用方法

将其添加到你的composer.json文件中,然后运行 $ php composer.phar install 来获取最新版本。

{
  "require": {
    "habanero/tailor": "dev-master"
  }
}

请记住,Neddle不会处理你的视图,它只将模板预编译成PHP代码。你应该保存并执行生成的代码。

<?php

require 'vendor/autoload.php';

# raw handling
$tpl = 'a { href => "#" } = "link"';
$out = Neddle\Parser::render($tpl);

eval('; ?' . ">$out"); // <a href="#">link</a>


# using as helper
function view($file, array $vars = array()) {
  ob_start();

  $tpl = file_get_contents($file);
  $_tpl = tempnam('/tmp', md5($file));
  file_put_contents($_tpl, Neddle\Parser::render($tpl));

  extract($vars);
  require $_tpl;
  unlink($_tpl);

  $out = ob_get_clean();

  return $out;
}


# full-view rendering
$tpl = 'example.neddle';
$out = view($tpl, array('name' => 'Joe'));

echo $out; // <p>Hello Joe!</p>

以下是 example.neddle 文件的源代码。

p = "Hello $name!"

想要合作吗?

如果你喜欢,可以Fork、下载并使用它。