beastbytes / yii2-microformats
从模型数据生成 microformats2 的 Yii2 小部件
v1.0.0
2015-07-26 16:12 UTC
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-09-19 09:16:27 UTC
README
本扩展是用于从模型数据生成 microformats 的 Yii2 小部件;还包括用于格式化 WGS84 坐标的 Formatter 类。
有关许可证信息,请查看 LICENSE 文件。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist beastbytes/yii2-microformats
或者
"beastbytes/yii2-microformats": "~1.0.0"
将其添加到您的 composer.json 文件的 require 部分。
使用方法
您可以使用此扩展在视图中以类似于 yii\widgets\DetailView 的方式使用(它是 DetailView 的子类)。
让我们从一个用户模型 - $user - 中生成一个 "h-card",该模型具有以下关系
- profile - 存储用户的姓名
- address - 存储用户的地址
- phone - 存储用户的电话号码
在视图中
echo Microformat::widget([ 'microformat' => 'h-card', // name of the microformat 'model' => $user, // the model that provides the data 'attributes' => [ / the attributes array specifies the microformat properties 'p-name:profile.name', // get the user's name from the profile relation 'p-email', 'p-tel:phone.value', [ // Additional markup that is not a microformat property 'value' => 'Address', 'template' => '<div class="h-card__title">{value}</div>' // with it's own template ], [ 'property' => 'p-adr', 'microformat' => 'h-adr, // Markup the address with an embedded h-adr microformat 'model' => $user->address, // we can specify a new model for embedded microformats; if not given the parent microformat model is used 'template' => '<div {options}>{value}</div>', // a new template for h-adr 'attributes' => [ 'p-street-address', // fetch data from $user->address->street_address 'p-locality', 'p-region', 'p-postal-code', [ 'property' => 'p-geo', 'microformat' => 'h-geo', 'template' => '<div><span>{label}</span><data {options} value="{rawValue}">{value}</data></div>', 'formatter' => [ // use the included formatter for latitude and longitude 'class' => '\\beastbytes\\microformats\\Formatter', 'coordinateFormat' => '%02d %02.6f h' ], 'attributes' => [ 'property' => 'p-latitude:latitude:latitude', // microformat property:model attribte:format 'property' => 'p-longitude:longitude:longitude' ] ] ] ] ] ]);
查看源代码以获取更多配置细节。