Skip to content

Slices

Slices in Prismic are reusable content blocks that can be dynamically arranged in your documents. The module provides specialized handling for Prismic slices.

Understanding Slices

Slices allow content editors to: - Build flexible page layouts - Reuse content components - Maintain consistent styling - Create dynamic content zones

Implementation

Slice Block

Elgentos\PrismicIO\Block\Slices

The Slices block handles rendering of slice content from Prismic documents.

Basic Usage

<block class="Elgentos\PrismicIO\Block\Slices" name="page.slices" template="body">
    <block class="Elgentos\PrismicIO\Block\Container" name="slice.container">
        <arguments>
            <argument name="reference" xsi:type="string">primary</argument>
        </arguments>
        <!-- Slice content blocks -->
    </block>
</block>

Slice Machine Integration

The module provides Slice Machine support through console commands:

# Initialize Slice Machine
bin/magento elgentos:prismic:slice-machine:init --store-code=<store>

# Start Slice Machine development
bin/magento elgentos:prismic:slice-machine:start --store-code=<store>

Configuration

Slice Template Structure

templates/
├── slices/
│   ├── text_block.phtml
│   ├── image_gallery.phtml
│   └── product_showcase.phtml
└── slice-wrapper.phtml

Slice Layout Example

<block class="Elgentos\PrismicIO\Block\Slices" name="content.slices" template="content">
    <!-- Text Block Slice -->
    <block class="Elgentos\PrismicIO\Block\Container" name="text.block">
        <block class="Elgentos\PrismicIO\Block\Dom\RichText" 
               name="text.content" 
               template="content"/>
    </block>

    <!-- Image Gallery Slice -->
    <block class="Elgentos\PrismicIO\Block\Container" name="gallery">
        <block class="Elgentos\PrismicIO\Block\Dom\Image" 
               name="gallery.images" 
               template="gallery"/>
    </block>
</block>

Creating Custom Slices

  1. Define slice in Slice Machine:

    {
      "text_block": {
        "type": "Slice",
        "fieldset": "Text Block",
        "description": "A basic text block",
        "icon": "text_fields",
        "non-repeat": {
          "content": {
            "type": "StructuredText",
            "config": {
              "multi": "paragraph,heading1,heading2",
              "label": "Content"
            }
          }
        }
      }
    }
    

  2. Create template file:

    <?php
    /** @var $block \Elgentos\PrismicIO\Block\Template */
    $slice = $block->getContext();
    ?>
    <div class="text-block">
        <?= $block->getChildHtml('text.content') ?>
    </div>
    

  3. Add layout configuration:

    <block class="Elgentos\PrismicIO\Block\Container" name="slice.text_block">
        <block class="Elgentos\PrismicIO\Block\Dom\RichText" 
               name="text.content" 
               template="slices/text_block"/>
    </block>
    

Best Practices

  1. Slice Organization
  2. Group related fields
  3. Use clear naming conventions
  4. Document slice purpose
  5. Keep slices focused

  6. Development

  7. Use Slice Machine for development
  8. Test slices across viewports
  9. Maintain consistent styling
  10. Consider reusability

  11. Performance

  12. Optimize slice loading
  13. Cache where appropriate
  14. Monitor resource usage
  15. Handle large slice zones

  16. Maintenance

  17. Document slice variations
  18. Keep styling consistent
  19. Update slice schemas
  20. Monitor slice usage

Troubleshooting

  1. Slices Not Rendering
  2. Verify slice configuration
  3. Check template paths
  4. Validate slice data
  5. Review block structure

  6. Styling Issues

  7. Check CSS scope
  8. Verify class names
  9. Review responsive behavior
  10. Test cross-browser

  11. Development Issues

  12. Validate Slice Machine setup
  13. Check store configuration
  14. Review console errors
  15. Verify slice schemas