Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ WPify Models, so we can access data easily and use them wherever we need.

Links: [WPify Model](https://packagist.org/packages/wpify/model)

### 05 - WPify Templates

[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/05-templates)
We want to be able to render templates easily, even in Twig. In this step, we are adding WPify Templates.

Links: [WPify Templates](https://packagist.org/packages/wpify/templates)


[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/06-assets-utils)
10 changes: 9 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use WpifyPluginTutorialDeps\DI\NotFoundException;
use WpifyPluginTutorial\Plugin;
use WpifyPluginTutorialDeps\Wpify\Model\Manager;
use WpifyPluginTutorialDeps\Wpify\Templates\TwigTemplates;
use WpifyPluginTutorialDeps\Wpify\Templates\WordPressTemplates;

use function WpifyPluginTutorialDeps\DI\create;

Expand All @@ -39,7 +41,13 @@ function wpifypt_container(): Container {
if ( empty( $container ) ) {
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions( array(
Manager::class => create()->constructor( array() )
WordPressTemplates::class => create()->constructor( array(
plugin_dir_path( __FILE__ ) . 'templates',
) ),
TwigTemplates::class => create()->constructor( array(
plugin_dir_path( __FILE__ ) . 'templates',
) ),
Manager::class => create()->constructor( array() )
) );
$container = $containerBuilder->build();
}
Expand Down
3 changes: 2 additions & 1 deletion composer-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"php": "^8.0",
"php-di/php-di": "^7.0",
"wpify/custom-fields": "^3.9",
"wpify/model": "^4.0"
"wpify/model": "^4.0",
"wpify/templates": "^3.0"
}
}
7 changes: 6 additions & 1 deletion src/Blocks/BookLinkBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use WpifyPluginTutorial\Managers\BlocksManager;
use WpifyPluginTutorial\Repositories\BookRepository;
use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields;
use WpifyPluginTutorialDeps\Wpify\Templates\TwigTemplates;
use WpifyPluginTutorialDeps\Wpify\Templates\WordPressTemplates;

class BookLinkBlock {
const KEY = 'wpify/book-link';

public function __construct(
CustomFields $custom_fields,
private BookRepository $book_repository,
private WordPressTemplates $wp_template,
// private TwigTemplates $twig_template,
) {
$custom_fields->create_gutenberg_block( array(
'name' => self::KEY,
Expand Down Expand Up @@ -46,6 +50,7 @@ public function render( $block_attributes, $content ) {
$block_attributes['isbn'] = $book->isbn;

// return the string with the rendered block.
return '<h1 id="' . $block_attributes['anchor'] . '">Book: ' . $block_attributes['title'] . ' (' . $block_attributes['isbn'] . ')</h1>';
return $this->wp_template->render( 'blocks/book-link', null, $block_attributes );
// return $this->twig_template->render( 'blocks/book-link.twig', null, $block_attributes );
}
}
22 changes: 22 additions & 0 deletions templates/blocks/book-link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @var array $args
*/

$id = $args['anchor'] ?? null;
$title = $args['title'] ?? null;
$isbn = $args['isbn'] ?? null;

if ( ! $title ) {
return;
}
?>
<h1<?= $id ? 'id="' . esc_attr( $id ) . '"' : '' ?>>
<?php
echo esc_html( $title );

if ( $isbn ) {
echo ' (' . esc_html( $isbn ) . ')';
}
?>
</h1>
7 changes: 7 additions & 0 deletions templates/blocks/book-link.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 id="{{ anchor }}">
{{ title }}

{% if isbn %}
({{ isbn }})
{% endif %}
</h1>