becklyn/schema-org

安装数: 3,814

依赖项: 0

建议者: 0

安全: 0

星标: 4

关注者: 4

分支: 1

公开问题: 0

类型:symfony-bundle

1.7.0 2022-02-17 15:54 UTC

This package is auto-updated.

Last update: 2024-09-17 21:44:03 UTC


README

此包提供了一种薄集成,用于渲染与Schema.org兼容的JSON-LD元数据标签。

创建schema

在您可以在模板中渲染元数据之前,您需要为每个您想生成JSON-LD schema的实体类型实现一个SchemaBuilderInterface

一个SchemaBuilderInterface实现的示例将如下所示,它将MyNewsArticle转换为Schema.org的Article

use App\Entity\MyNewsArticle;
use Becklyn\SchemaOrg\Data\Article;
use Becklyn\SchemaOrg\Data\Organization;
use Becklyn\SchemaOrg\Data\SchemaOrgDataInterface;
use Becklyn\SchemaOrg\SchemaBuilder\SchemaBuilderInterface;

class MyNewsSchemaBuilder implements SchemaBuilderInterface
{
    /**
     * @inheritDoc
     */
    public function supports ($entity, ?string $usage = null, array $context = []) : bool
    {
        return $entity instanceof MyNewsArticle;
    }


    /**
     * @inheritDoc
     */
    public function buildSchema ($entity, ?string $usage = null, array $context = []) : ?SchemaOrgDataInterface
    {
        \assert($entity instanceof MyNewsArticle);

        // Don't generate metadata for unpublished news
        if (!$entity->isPublished())
        {
            return null;
        }

        return (new Article())
            ->withEditor($entity->getAuthor())
            ->withAbout($entity->getTeaserText())
            ->withArticleBody($entity->getTextContent())
            ->withPublisher(
                (new Organization())
                    ->withName("Awesome Publisher")
                    ->withEmail("news@example.com")
            )
            ->withLicense("Creative Commons Attribution-ShareAlike 3.0")
        ;
    }
}

渲染元数据标签

要渲染JSON-LD元数据标签,调用Twig函数{{ schema_org_meta_data(myAppEntity) }},并传递可选的usagecontext参数,如果您需要根据当前的渲染上下文调整输出。