daun/statamic-latte

在Statamic网站上使用Latte模板

安装: 317

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 1

开放问题: 0

类型:statamic-addon

1.1.1 2024-04-29 14:16 UTC

This package is auto-updated.

Last update: 2024-09-29 15:06:21 UTC


README

Latest Version on Packagist Test Status Code Coverage License

Statamic网站上使用Latte模板语言。

✨ 特性

  • 渲染.latte视图
  • 使用Statamic内置标签和修饰符
  • 从条目数据中解析当前布局
  • 在有用处渲染Antlers内联

🛠️ 安装

从您的项目根目录运行以下命令

composer require daun/statamic-latte

或者,您可以在Statamic控制面板的工具 > 扩展部分中搜索此扩展并从那里安装。

🎨 使用

安装后,您就可以在前端使用Latte视图了。只需将视图保存或重命名为使用扩展.latte,并按常规引用它们。当然,只要确保只有一个同名视图即可,您可以使用它们并排使用。

- /resources/views/project.antlers.html
+ /resources/views/project.latte

Antlers

<ul>
    {{ songs }}
        <li>{{ value }} (Next: {{ next:value }})</li>
    {{ /songs }}
</ul>

Latte

<ul n:inner-foreach="$songs as $song">
    <li>{$song} (Next: {$iterator->nextValue})</li>
</ul>

标签

Statamic标签可以通过s辅助函数使用

Antlers

{{ collection:pages take="8" }}
  {{ title }}
{{ /collection }}

Latte

{foreach s('collection:pages', take: 8) as $entry}
  {$entry->title}
{/foreach}

修饰符

Statamic修饰符也可以在Latte中作为过滤器使用

Antlers

<h1>{{ title | upper | truncate(50) }}</h1>

Latte

<h1>{$title|upper|truncate:50}</h1>

混合与匹配

如果您需要结合Latte和Antlers代码,您可以在Latte视图中使用antlers标签来内联渲染Antlers代码。这对于复杂的内置标签或通过从文档中复制示例进行快速原型设计很有用。

Rendered in Latte: {$title}

{antlers}
    Rendered in Antlers: {{ title }}
{/antlers}

布局

就像在Antlers模板中一样,将根据您的条目和蓝图中的可用数据使用正确的布局文件。

默认情况下,它将查找/resources/views/layout.latte,但您可以通过在条目或集合配置文件中设置layout: other_layout来配置特定的条目和集合使用不同的布局。

缓存

缓存

使用cache标签来缓存视图的一部分。

{cache for: '10 minutes'}
    {foreach $stocks as $stock}
        {$stock->fetchPrice()}
    {/foreach}
{/cache}

Nocache

可以使用nocache标签将视图的一部分免除静态缓存

{include 'partials.nav', handle: main}
 
{nocache} 
    {if $logged_in}
        Welcome back, {$current_user->name}
    {else}
        Hello, Guest!
    {/if}
{/nocache}
 
{block content}{/block}

限制

nocache标签仅支持应用程序级别的静态缓存。完整的基于文件的缓存需要JavaScript才能使nocache正常工作,这在该插件中尚未实现。有关详细信息,请参阅缓存策略

cachenocache的嵌套也尚未支持。以下内容将无法正常工作

{cache}
    this will be cached
    {nocache}
        this will remain dynamic
    {/nocache}
    this will also be cached
{/cache}

许可证

MIT