All Projects → SchumacherFM → Magento2-Twig

SchumacherFM / Magento2-Twig

Licence: other
Twig Template Engine for Magento2

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Magento2-Twig

magento-grid-colors
Magento 2 Grid Colors module for colorizing admin grids. Supports saving of states with the help of grid's bookmarks.
Stars: ✭ 54 (-6.9%)
Mutual labels:  magento, magento2
module-wysiwyg-advanced
Extend TinyMCE 4 & 5 tools in Magento 2
Stars: ✭ 60 (+3.45%)
Mutual labels:  magento, magento2
module-blog-sample-data
Sample data for Magento 2 blog extension
Stars: ✭ 15 (-74.14%)
Mutual labels:  magento, magento2
module-catalog
Fixes for some known Magento 2 issues in Catalog
Stars: ✭ 23 (-60.34%)
Mutual labels:  magento, magento2
bestsellers-sorting-m2
Bestsellers sorting of products in categories for Magento 2
Stars: ✭ 24 (-58.62%)
Mutual labels:  magento, magento2
magento2-module-disabletwofactorauth
The DisableTwoFactorAuth module provides the ability to disable two-factor authentication.
Stars: ✭ 147 (+153.45%)
Mutual labels:  magento, magento2
nginx-virtual-host-bash-script
Nginx Virtual Host Bash Script
Stars: ✭ 35 (-39.66%)
Mutual labels:  magento, magento2
magento-2-infinite-scroll
Magepow Infinite Scroll extension Free hepls website loading content and products without reloading page
Stars: ✭ 17 (-70.69%)
Mutual labels:  magento, magento2
DiscountCodeUrl
Magento 2 module that applies a discount code automatically via URL
Stars: ✭ 33 (-43.1%)
Mutual labels:  magento, magento2
taxjar-magento2-extension
Magento 2 Sales Tax Extension by TaxJar
Stars: ✭ 23 (-60.34%)
Mutual labels:  magento, magento2
magento2-cache-permissions
A module for Magento 2 to add access control restrictions to the cache management.
Stars: ✭ 16 (-72.41%)
Mutual labels:  magento, magento2
module-blog-comments-recaptcha
module-blog-comments-recaptcha
Stars: ✭ 17 (-70.69%)
Mutual labels:  magento, magento2
phpstorm-magento-live-templates
PHPStorm Live Templates for Magento 2
Stars: ✭ 49 (-15.52%)
Mutual labels:  magento, magento2
magento2-checkout-success-misc-script
Add Miscellaneous HTML and JavaScript codes to Magento2 checkout conformation page
Stars: ✭ 26 (-55.17%)
Mutual labels:  magento, magento2
module-conflict-detector
magefan.com/magento2-conflict-detector
Stars: ✭ 40 (-31.03%)
Mutual labels:  magento, magento2
pimcore-magento2-bridge
Pimcore module for Magento 2 integration
Stars: ✭ 33 (-43.1%)
Mutual labels:  magento, magento2
magento2-module-simpledata
The SimpleData module simplifies calling Magento data structures.
Stars: ✭ 82 (+41.38%)
Mutual labels:  magento, magento2
m2devbox
🚀 Head start your Magento 2 extension development with pre-configured Docker environments.
Stars: ✭ 18 (-68.97%)
Mutual labels:  magento, magento2
magento
Free PWA & SPA for Magento
Stars: ✭ 34 (-41.38%)
Mutual labels:  magento, magento2
hubble-frontend-pwa
E-Commerce PWA Frontend
Stars: ✭ 43 (-25.86%)
Mutual labels:  magento, magento2

Magento 2 Twig Template Engine

Twig template engine for Magento2.

This template engine is meant to be used additionally to the .phtml files and does not provide any .twig template file.

A use case would be if you write your first Magento2 module you can require this package and write all your template files in Twig.

Installation

  1. Add dependency
composer require schumacherfm/magento-twig
  1. Enable the module
bin/magento module:enable SchumacherFM_Twig
  1. Update the database entries
bin/magento setup:upgrade

Events & Configuration

The Twig template engine class dispatches two events so that you can modify Twig.

Event twig_loader with event object loader. You can set loader any other class which implements Twig_LoaderInterface. http://twig.sensiolabs.org/doc/api.html#loaders

Event twig_init with event object twig. You can add here more functions, filters, tags, etc. http://twig.sensiolabs.org/doc/advanced.html

Configuration options can be found Stores -> Settings -> Configuration -> Advanced -> Developer -> Twig.

Frontend Integration

Your template files must have the file extension .twig to get automatically recognized.

In your layout xml files or blocks please specify the new template

<referenceBlock name="top.links">
    <block class="Magento\Theme\Block\Html\Header" template="html/header.twig" name="header" as="header" before="-">
        <arguments>
            <argument name="show_part" xsi:type="string">welcome</argument>
        </arguments>
    </block>
</referenceBlock>

Example header.phtml converted to header.twig

<?php switch ($this->getShowPart()):
    case 'welcome': ?>
        <li class="greet welcome"><?php echo $this->getWelcome() ?></li>
    <?php break; ?>
    <?php case 'other': ?>
        <?php echo $this->getChildHtml(); ?>
    <?php break; ?>
<?php endswitch; ?>
{% if getShowPart() == 'welcome' %}
    <li class="greet welcome">{{ getWelcome() }}</li>
{% endif %}

{% if getShowPart() == 'other' %}
    {{ getChildHtml()|raw }}
{% endif %}

Example breadcrumbs.phtml converted to breadcrumbs.twig

<?php if ($crumbs && is_array($crumbs)) : ?>
<div class="breadcrumbs">
    <ul class="items">
        <?php foreach ($crumbs as $crumbName => $crumbInfo) : ?>
            <li class="item <?php echo $crumbName ?>">
            <?php if ($crumbInfo['link']) : ?>
                <a href="<?php echo $crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($crumbInfo['title']) ?>">
                    <?php echo $this->escapeHtml($crumbInfo['label']) ?>
                </a>
            <?php elseif ($crumbInfo['last']) : ?>
                <strong><?php echo $this->escapeHtml($crumbInfo['label']) ?></strong>
            <?php else: ?>
                <?php echo $this->escapeHtml($crumbInfo['label']) ?>
            <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>
{% if crumbs %}
<div class="breadcrumbs">
    <ul class="items">
    {% for crumbName,crumbInfo in crumbs %}
        <li class="item {{ crumbName }}">
            {% if crumbInfo.link %}
                <a href="{{ crumbInfo.link }}" title="{{ crumbInfo.title }}">
                    {{ crumbInfo.label }}
                </a>
            {% elseif crumbInfo.last %}
                <strong>{{ crumbInfo.label }}</strong>
            {% else %}
                {{ crumbInfo.label }}
            {% endif %}
        </li>
    {% endfor %}
    </ul>
</div>
{% endif %}

Access helper methods

Write in your .twig file:

{{ helper("Magento\\Core\\Helper\\Url").getHomeUrl() }}

Tests

@todo

Support / Contribution

Report a bug using the issue tracker or send us a pull request.

Instead of forking I can add you as a Collaborator IF you really intend to develop on this module. Just ask :-)

I am using that model: A successful Git branching model

For versioning have a look at Semantic Versioning 2.0.0

History

2.0.0

  • Added Magento 2.4.0 compatibility
  • Removed helper functions from app/functions.php since the file is no longer available in Magento 2.4
  • Removed deprecated function layoutBlock from twig environment
  • Updated to twig to 3.0.*

Compatibility

  • Magento >= 2
  • php >= 5.4.0

License

OSL-30

Author

Cyrill Schumacher

My pgp public key

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].