ProcessWire模板引擎和ExpressionEngine的语法对比
PW没有使用当下流行的php模板引擎如Twig、smarty,而是采用的php原生语法,这极大程度地降低了学习成本并提高了开发效率和可维护性,下面是官方提供的和ExpressionEngine CMS模板引擎语法对比的代码片段。
示例一
ExpressionEngine
{embed=layout/header} <h1>{exp:channel:category_heading channel="reviews"}{category_name}{/exp:channel:category_heading}</h1> {exp:channel:entries channel="reviews"} <article class="review"> <h2><a href="{url_title_path=reviews}">Reviews in {title}</a></h2> <p class="meta">Posted on {entry_date format="%Y %m %d"}</p> <p>{content}</p> </article> {/exp:channel:entries} {/embed=layout/footer}
ProcessWire 示例代码一:内容完全由php输出
include("header"); e("<h1>{$page->category->title}</h1>"); foreach($pages->find("parent=/reviews/") as $review) e(" <article class='review'> <h2><a href='{$review->url}'>Reviews in {$review->title}</a></h2> <p class='meta'>Posted on {$review->date}</p> <p>{$review->content}</p> <article>"); include("footer");
注意:{$review->date}
输出日期的格式可在后台设置,在模板中直接输出
ProcessWire示例代码二:HTML混合php语法
(功能同上,大多数人选择这种语法,但个人更喜欢前者)
<? include("./header.inc"); ?> <h1><?=$page->category->title?></h1> <? foreach($pages->get("/reviews/")->children() as $review: ?> <article> <h2><a href="<?=$review->url?>">Reviews in <?=$review->title?></a></h2> <p class="meta">Posted on <?=$review->date?></p> <p><?=$review->content?></p> </article> <? endforeach; ?> <? include("./footer.inc"); ?>
示例二
ExpressionEngine
{exp:channel:entries channel='faq'} {if count == 1} <ul> {/if} <li><a href="{title_permalink=faq/entry}">{title}</a></li> {if count == absolute_results} </ul> {/if} {if no_results} <h1>No entries found</h1> {/if} {/exp:channel:entries}
$li = ''; foreach($pages->find("parent=/faq/") as $e) $li .= "<li><a href='{$e->url}'>{$e->title}</a></li>"; echo $li ? "<ul>$li</ul>" : "<h1>No entries found</h1>";