Templates
Templates define how your Prismic content is rendered in Magento 2. Here's how to work with templates effectively.
Template Structure
A typical Prismic template consists of multiple parts:
- The main template file
- Element templates for specific content blocks
- Layout configuration that ties it all together
Example Implementation
Here's a complete example showing how to structure templates for a homepage:
- Create your main template (
homepage.phtml
):
<?php declare(strict_types=1);
/** @var $block \Elgentos\PrismicIO\Block\Template */
?>
<section class="content-wrap">
<div class="grid lg:grid-cols-6">
<?php echo $block->getChildHtml('page.body'); ?>
</div>
</section>
- Create your element template (
element/homepage-block.phtml
) for individual content blocks:
<?php declare(strict_types=1);
/** @var $block \Elgentos\PrismicIO\Block\Template */
$homepageBlock = $block->getContext();
?>
<div class="block <?= $block->getChildHtml('block.width') ?>">
<a href="<?= $block->getChildHtml('block.link') ?>">
<img src="<?= $block->getChildHtml('block.image') ?>" alt="">
</a>
</div>
Best Practices
- Keep templates focused on presentation logic
- Use meaningful template names that reflect their purpose
- Organize templates in appropriate directories based on their function
- Use PHP DocBlocks to properly type-hint block classes
- Leverage block methods like
getChildHtml()
for structured content - Keep presentation logic in templates and structural logic in blocks