hdytsgt/twigson-bundle

Symfony 3 Bundle 用于 Twig 过滤器扩展,用于读取 JSON 文件或通过 URL

dev-master / 1.0.x-dev 2016-08-03 01:27 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:03:57 UTC


README

Symfony Bundle 用于 Twig 过滤器扩展,用于读取本地 JSON 文件或直接通过 URL。

安装

确保您已经在系统中全局安装了 Composer。然后进入您的终端,在您的 Symfony 项目中运行以下命令

composer require hdytsgt/twigson-bundle:dev-master

当 Composer 完成安装包后,前往您的 app/AppKernel.php 并将以下类 new hdytsgt\TwigsonBundle\TwigsonBundle() 添加到 registerBundles() 中,示例

public function registerBundles()
{
        $bundles = [
            ...
            new hdytsgt\TwigsonBundle\TwigsonBundle(),
        ];
}

用法

  1. 使用本地 JSON 文件

    {% set json = './countries.json' | twigson %}
    
    {% for key, item in json %}
    
       {{ key }} : {{ item }}
       
    {% endfor %}

    这将找到位于您的 Symfony web 文件夹下的 countries.json 文件。当然,您也可以传递绝对路径。

  2. 使用 URL

    {% set json = 'http://yoursite.com/countries.json' | twigson %}
    
    {% for key, item in json %}
    
       {{ key }} : {{ item }}
       
    {% endfor %}

相关项目

Twigson for Craft CMS

许可

MIT