MailWizz - Twig Template Engine Template Includes

Mark R. White

New Member
Does MailWizz's implementation of the Twig support include statements (as seen below)?

Code:
{{ include('sidebar.html') }}

{% for box in boxes %}
    {{ include('render_box.html') }}
{% endfor %}

If so where would you save those partial files? through the front end or just store them in twig's source folder?

If not, is there any plan to do so?
 
That's a good question actually.
We do not actively support this feature because of the issue it might cause,
However, we have a hook that you can connect to and alter twig to make it load your includes, like:
PHP:
# see https://twig.symfony.com/doc/1.x/api.html
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
    $loader = $twig->getLoader();
    $loader->addPath($templateDir);
    return $twig;
});
You can also discard the twig instance we're creating and create your own:
PHP:
# see https://twig.symfony.com/doc/1.x/api.html
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
   
    $loader = new Twig_Loader_Filesystem('/path/to/templates');
    $twig = new Twig_Environment($loader, array(
       'cache' => '/path/to/compilation_cache',
    ));
    return $twig;
});
 
PHP:
# see https://twig.symfony.com/doc/1.x/api.html
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
    $loader = $twig->getLoader();
    $loader->addPath($templateDir);
    return $twig;
});
I'm not familiar with Yii and how it's structured. Could you point me to the file/function this code should be added to?
 
Create a file named init-custom.php in the /apps/ folder and put the code there.
Please note that i haven't tested the code, you'd have to play with it.
 
That's a good question actually.
We do not actively support this feature because of the issue it might cause,
However, we have a hook that you can connect to and alter twig to make it load your includes, like:
PHP:
# see https://twig.symfony.com/doc/1.x/api.html
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
    $loader = $twig->getLoader();
    $loader->addPath($templateDir);
    return $twig;
});
You can also discard the twig instance we're creating and create your own:
PHP:
# see https://twig.symfony.com/doc/1.x/api.html
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
 
    $loader = new Twig_Loader_Filesystem('/path/to/templates');
    $twig = new Twig_Environment($loader, array(
       'cache' => '/path/to/compilation_cache',
    ));
    return $twig;
});

---

After some trial and error this worked for me:
• create new template folder (e.g., './apps/common/data/twig/templates')
• add template file to that folder (e.g., 'template-test.html')
• add custom hook to enable Twig template includes (e.g., './apps/init-custom.php' *code shown below)

PHP:
<?php // './apps/init-custom.php'
Yii::app()->hooks->addFilter('twig_create_instance', function($twig){
  $templateDir = MW_APPS_PATH . '/' . 'common/data/twig/templates';
  $loader = new Twig_Loader_Filesystem($templateDir);
  $twig = new Twig_Environment($loader, array(
    'cache' => false,
    'auto_reload' => true,
  ));
  return $twig;
});

*side note: the following lines of code were straight copy/paste and I don't know what they do and haven't tested to see. Maybe someone more familiar could explain?

PHP:
    'cache' => false,
    'auto_reload' => true,

• use template in your mailwizz email template (i.e., create a template via MailWizz's frontend UI that will call for (or 'include') one of your new template files.
HTML:
{{ include('template-test.html') }}

*Edited templateDir path to avoid template folder being overwritten on MailWizz upgrades per @twisted1919's advice. 2017-11-21@9:40am
 
Last edited:
Maybe you'll want to move the templatesDir to another location, maybe in common/data/twig or something that won't be overwritten at upgrades.
 
I enabled URL tracking for my campaign, however the URLs remain unchanged when included via the Twig template engine. Am I doing something wrong or is it that I should change the the URLs ~manually because I've enabled/used Twig's include?

If I need to modify the URLs myself could you provide an example please?
 
however the URLs remain unchanged when included via the Twig template engine
Hmm, that's a good question, i think the include happens after mailwizz parses the links therefore mailwizz doesn't get a chance to parse the urls and prepare them for tracking, case in which, you should also implement the parsing process in your own twig logic :-s
 
Hmm, that's a good question, i think the include happens after mailwizz parses the links therefore mailwizz doesn't get a chance to parse the urls and prepare them for tracking, case in which, you should also implement the parsing process in your own twig logic :-s
How could this be disentangled?
 
@frm.mwz - we don't really plan to support this feature, of loading twig templates because it simply isn't something such app should do.
The fact this is possible now is just a happy side effect of using twig, but for anything more, whoever wants it has to put some work to make it happen.
 
Back
Top