rodnaph/morgan

PHP纯HTML模板

v0.2.0 2013-01-19 10:15 UTC

This package is auto-updated.

Last update: 2024-09-07 18:15:58 UTC


README

Morgan是一个用于PHP的轻量级模板库,它可以启用使用'纯HTML'模板。这意味着您的HTML文件中不包含任何模板,它们是独立组件,可以独立查看和编辑。然后Morgan使用您的应用程序数据处理这些模板的转换。

Morgan是EnLive想法的PHP端口。

使用方法

如果您想直接开始,有一个示例应用程序包含更多详细信息。

use Morgan\Template as T;

T::render(
    'page.html',
    array(
        '.header h1' => T::content('Some Text')
    )
);

在这里,我们使用一个HTML文件来渲染一个模板。当我们调用render时,我们传递源文件名,以及一个键值对数组,其中键是文档中元素的CSS选择器,值是转换这些元素的函数。

所以在上面的例子中,我们选择了.header元素内的所有h1元素,并将它们的内容设置为字符串'Some Text'。T::render()将输出模板内容,您也可以使用T::fetch()仅返回转换后的内容。

您还可以像这样获取可调用模板函数的句柄...

$homePage = T::template(
    'index.html',
    function($title, $content) {
        return array(
            'h1' => T::content($title),
            '.container' => T::htmlContent($content)
        );
    }
);

$html = $homePage('The Title', '<h2>Main content...</h2>');

转换器

在上面的简单示例中,我们使用的转换器是content转换器。还有其他可用的转换器...

# set the content of an element
T::content('Some new content')

# append content to an element
T::append('This please')

# prepend content to an element
T::prepend('Some string')

# use HTML as content for an element
T::htmlContent('<b>bold text</b>')

# set attributes of elements
T::setAttr('href', '/blog/post.html')

# remove attributes of elements
T::removeAttr('class')

# add classes to element
T::addClass('foo', 'bar', 'baz')

# remove classes from element
T::removeClass('foo', 'bar', 'baz')

# replace with some HTML
T::replaceWith('<b>Some HTML</b>')

自定义转换器

您还可以创建自己的转换器,它们只是接受DOMElement对象并对其进行某种方式修改的函数。

T::render(
    'file.html',
    array(
        'a' => function(DOMElement $element) { … }
    )
);

多转换

通常您会希望将多个转换器应用到给定的选择器上。您可以通过使用all形式来实现这一点。

array(
    '.foo' => T::all(
        T::content('New content'),
        T::setAttr('href', '/some/page.html')
    )
)

片段

除了整个文档外,您还可以创建片段。这些是文档的片段,您可以使用它们来进行诸如从HTML文件中提取博客文章,然后在另一个模板中渲染此文章等活动。

$snippet = T::snippet(
    'blog-post-list.html',
    '.post',
    function($data) {
        return array(
            '.subject' => T::content($data['title'])
        );
    }
);

第二个参数是从文档中选择片段的CSS选择器。

更复杂的例子

作为一个功能更完整的例子,假设您有一个页面,您想要列出一些博客文章。这个页面将包含一个标题,然后是一系列博客文章的主题及其内容的简短摘要(标题链接到每篇文章)。

我们可以在单个名为blog-posts.html的文件中标记这些内容,然后将其用作页面的模板,提取用于摘要视图的博客文章示例片段。

# array of blog posts to show

$posts = array(
    array(
        'title' => 'First Post',
        'summary' => 'Some short snippet',
        'href' => '/blog/post-one.html'
    ),
    array(
        'title' => 'Another Post',
        'summary' => 'And another short snippet',
        'href' => '/blog/another-post.html'
    )
);

# re-usable blog post summary snippet

$postSnippet = T::snippet(
    'blog-posts.html',
    '.post',
    function($item) {
        return array(
            'h3 a' => T::all(
                T::content($item['title']),
                T::setAttr('href', $item['href'])
            ),
            'p' => T::content($item['summary'])
        );
    }
);

# render the main template

T::render(
    'blog-posts.html',
    array(
        'h1' => 'The Blog Posts Page',
        '.posts' => T::map($postSnippet, $posts)
    )
);

面向对象的使用

除了静态接口,如果您愿意,还可以以面向对象的方式使用Morgan。

use Morgan\Template as T;

# create and echo the template

$t = new T('path/to/file.html');

echo $t->html(array(
    'h1' => T::contect('Some title')
));

# snippet is exactly the same, but with a selector

$s = new T('path/to/file.html', '.some-selector');

echo $s->html(array(
    '.description' => T::content('A Description')
));

使用Composer安装

Morgan可通过Composer获取,只需要求它并指定您想要使用的版本

composer require rodnaph/morgan

动机

这个库是受EnLive启发的,主要是PHP中的一个仅限娱乐实现。如果您发现它有用,请随时贡献!

待办事项

以下来自EnLive的功能尚未实现,因此我还需要审查它们,如果它们有意义的话,我将进行实现...

wrap
unwrap
after
before
move