alanablett/twig-faker
Composer 安装命令:
composer require alanablett/twig-faker
包简介
Integration of PHP faker library with the twig templating system.
README 文档
README
TwigFaker
An extension to add the faker library into twig templates, providing a fast efficient way to create dummy data within your templates. Here's a quick example:
Configure your fake data type factories/person.php
<?php return [ 'first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'avatar' => $faker->imageUrl(300, 300) ];
Use in your templates
{% for person in fake('person', 20) %}
<p>Hello {{ person.first_name }} {{ person.first_name }}</p>
<img src="{{ person.avatar }}">
{% endfor %}
Usage
Step 1: Install
Pull this package in through Composer.
"require": { "alanablett/twig-faker": "~1.1" }
Step 2: Register the TwigFaker extension with twig
$twig->addExtension(new Ablett\TwigFaker\TwigFakerExtension);
Step 3: Create a Factories File
TwigFaker isn't magic. You need to describe the type of data that should be generated.
Each factory file you create will automatically have access to a $faker variable. This is just a normal factory that you would create by doing Faker\Factory::create.
Within a factories directory in the root of your project, you may create any number of PHP files that will automatically be loaded by TwigFaker. Why don't you start with a generic factories/person.php file.
<?php return [ 'first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'avatar' => $faker->imageUrl(300, 300) ];
Step 4: Call your new factory in a twig template
With your new factory defined, you can now use it in your twig template.
{% for person in fake('person', 20) %}
<p>Hello {{ person.first_name }} {{ person.first_name }}</p>
<img src="{{ person.avatar }}">
{% endfor %}
The first parameter passed to the fake method is the factory name to be used. The second parameter is the number of fake instances you want to loop through (default is 1 if no parameter is passed).
Note: You can organise your factories in folders such as factories/subfolder/another/example.php
{% for example in fake('subfolder/another/example', 20) %}
<p>{{ example.value }}</p>
{% endfor %}
Usage With Sculpin
Step 1: Install
Pull this package in through Composer by adding it to the sculpin.json file.
"require": { "alanablett/twig-faker": "~1.1" }
Step 2: Register the TwigFaker extension with sculpin
To use the extension in sculpin we have to add it to the extensions that sculpin knows about. This is done by adding it to app/config/sculpin_kernel.yml
services: twig.extension.faker: class: Ablett\TwigFaker\TwigFakerExtension tags: - { name: twig.extension }
Follow the further steps above to create new factories and use them in your templates.
Other Features
Caching
As of version 1.1 you can now cache your fake data in order to use later on down the page. This can be particularly useful when your markup must exist in different areas, but must use the same data. For example I have a hero block factory defined as follows
<?php return [ 'title' => $faker->sentence(), 'background' => $faker->imageUrl(1700, 600) ];
Now in my markup I would like to use the same data for two different calls to the fake method, so I simply pass in a key as the third argument to ensure the data is the same, in this case main-heroes
{% for hero in fake('heroes', 4, 'main-heroes') %}
<div class="hero__cell" data-background-url="{{ hero.background }}">
<h2>{{ hero.title }}</h2>
</div>
{% endfor %}
...
...
...
{% for hero in fake('heroes', 4, 'main-heroes') %}
<div class="hero__button">
<button>{{ hero.title }}</button>
</div>
{% endfor %}
alanablett/twig-faker 适用场景与选型建议
alanablett/twig-faker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2015 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alanablett/twig-faker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alanablett/twig-faker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-18