Skip to content

Introduce #[AsBlock] attribute #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 18, 2025

Conversation

silasjoisten
Copy link
Collaborator

@silasjoisten silasjoisten commented Mar 17, 2025

This PR introduces a powerful new way to define Storyblok blocks in Symfony applications using the #[AsBlock] attribute. This feature enables developers to register blocks generically, ensuring consistency and simplifying block management.

Key Features

1. #[AsBlock] Attribute

  • Allows defining a block with an optional name and template.
  • If name is omitted, the system will automatically convert the class name to camelCase (e.g., SampleBlocksample_block).
  • The template parameter specifies the Twig template used for rendering. If omitted, a default template path is derived.

2. Default Template Path Configuration

  • Blocks are rendered using a default path:
    blocks/{name}.html.twig
    
  • This can be customized via configuration:
    # config/packages/storyblok.yaml
    storyblok:
        blocks_template_path: 'my/custom/path'

3. New Twig Extension: BlockExtension

  • Provides the render_block filter for rendering Storyblok blocks effortlessly.
  • Example usage:
    {% for block in page.body %}
        {% if block is not null %}
            {{ block|render_block }}
        {% endif %}
    {% endfor %}

Example: Defining a Block with #[AsBlock]

namespace Storyblok\Bundle\Tests\Double\Block;

use Storyblok\Bundle\Block\Attribute\AsBlock;
use Webmozart\Assert\Assert;

#[AsBlock(technicalName: 'sample', template: 'custom_blocks/sample.html.twig')]
final readonly class SampleBlock
{
    public string $title;
    public string $description;

    public function __construct(array $values)
    {
        Assert::keyExists($values, 'title');
        $this->title = $values['title'];

        Assert::keyExists($values, 'description');
        $this->description = $values['description'];
    }
}

This class:

  • Registers a Storyblok block named sample_block.
  • Uses a custom template at custom_blocks/sample.html.twig.
  • Ensures proper structure via assertions.

This PR brings significant flexibility and ease to managing Storyblok blocks within Symfony, making integration more intuitive and robust.

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 89.15663% with 9 lines in your changes missing coverage. Please review.

Project coverage is 91.69%. Comparing base (5621062) to head (9043173).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/DependencyInjection/StoryblokExtension.php 50.00% 8 Missing ⚠️
src/DataCollector/StoryblokCollector.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #16      +/-   ##
============================================
- Coverage     92.20%   91.69%   -0.52%     
- Complexity       52       70      +18     
============================================
  Files             7       12       +5     
  Lines           231      313      +82     
============================================
+ Hits            213      287      +74     
- Misses           18       26       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@silasjoisten silasjoisten requested review from Copilot and edodusi March 17, 2025 12:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces the new #[AsBlock] attribute to register Storyblok blocks in Symfony applications and adds documentation on its usage, including default template path customization and a new Twig filter for rendering blocks.

  • Introduces the #[AsBlock] attribute to define Storyblok blocks.
  • Documents the default template path configuration and customizable settings.
  • Adds a new Twig filter, render_block, for dynamic block rendering.

@OskarStark
Copy link
Collaborator

I like it, except technicalName should be name

@silasjoisten
Copy link
Collaborator Author

I like it, except technicalName should be name

In storyblok its called technical_name, name is not possible anyway because the name property of an attribute is reserved by PHP

@OskarStark
Copy link
Collaborator

@silasjoisten silasjoisten force-pushed the feature/tiptap-storyblok-extension branch from 04c3599 to dbe57aa Compare March 18, 2025 05:53
silasjoisten and others added 2 commits March 18, 2025 07:55
Co-authored-by: Edoardo Dusi <[email protected]>
@silasjoisten
Copy link
Collaborator Author

https://github.com/php-llm/llm-chain/blob/main/src/Chain/ToolBox/Attribute/AsTool.php

why should it be reserved?

All good i changed it. i think i called the wrong variable that time.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces the #[AsBlock] attribute for defining Storyblok blocks in Symfony applications and updates the documentation accordingly.

  • Adds a new section to README.md for block registration and usage examples.
  • Provides guidance on configuring default template paths and rendering blocks in Twig.
Comments suppressed due to low confidence (1)

README.md:201

  • The parameter 'name' is used instead of 'technicalName' as described in the PR title and metadata. Update the parameter to 'technicalName' for consistency.
#[AsBlock(name: 'sample', template: 'custom_blocks/sample.html.twig')]

Copy link
Collaborator

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👏

@silasjoisten silasjoisten merged commit 0eb9f8e into master Mar 18, 2025
3 checks passed
@silasjoisten silasjoisten deleted the feature/tiptap-storyblok-extension branch March 18, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants