Loop in mail template

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
andreask
Registered User
Posts: 655
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Loop in mail template

Post by andreask »

Hello,

I am trying to have a loop in a mail template but for some reason I have some unwanted results...
What I mean?

Well first of all I generate 2 arrays that look like this...

Code: Select all

/srv/http/newnew/ext/andreask/ium/classes/reminder.php:115:
array (size=2)
  1 => 
    array (size=2)
      'title' => string 'Κεντρικό topic ευχών' (length=33)
      'url' => string 'http://new.local/viewtopic.php?f=94?&t=182' (length=42)
  2 => 
    array (size=2)
      'title' => string 'Σούπα' (length=10)
      'url' => string 'http://new.local/viewtopic.php?f=61?&t=365' (length=42)

/srv/http/newnew/ext/andreask/ium/classes/reminder.php:126:
array (size=5)
  0 => 
    array (size=2)
      'title' => string 'Σούπα' (length=10)
      'url' => string 'http://new.local/viewtopic.php?f=61?&t=365' (length=42)
  1 => 
    array (size=2)
      'title' => string 'Κεντρικό topic ευχών' (length=33)
      'url' => string 'http://new.local/viewtopic.php?f=94?&t=182' (length=42)
  2 => 
    array (size=2)
      'title' => string 'Τι πήρα - ήρθε σήμερα [13]' (length=42)
      'url' => string 'http://new.local/viewtopic.php?f=61?&t=5895' (length=43)
  3 => 
    array (size=2)
      'title' => string 'Τι πήρα - ήρθε σήμερα [14]' (length=42)
      'url' => string 'http://new.local/viewtopic.php?f=61?&t=6318' (length=43)
  4 => 
    array (size=2)
      'title' => string 'Το ξύρισμα της ημέρας : Μάρτιος 2018' (length=61)
      'url' => string 'http://new.local/viewtopic.php?f=113?&t=6031' (length=44)
And then I assign them to the template...
$messenger->assign_block_vars('USR_TPC_LIST', $topic_links);
and
$messenger->assign_block_vars('USR_FRM_LIST', $forum_links);

Both arrays have different data in them.

Then on the email template I have this "contraction"...

Code: Select all

<!-- IF USR_TPC_LIST -->
Below some links to topics that you've been active.
<!-- BEGIN USR_TPC_LIST -->
{% for user_topics in USR_TPC_LIST %}
    "{{ user_topics.title }}"
    {{ user_topics.url }}

{% endfor %}
<!-- END USR_TPC_LIST -->
<!-- ENDIF -->

<!-- IF USR_FRM_LIST -->
Below some links to the most active topics of the board.

<!-- BEGIN USR_FRM_LIST -->
{% for forum_topics in USR_FRM_LIST %}
    "{{ forum_topics.title }}"
    {{ forum_topics.url }}
{% endfor %}
<!-- END USR_FRM_LIST -->
For some reason now the e-mail that is being sent has some empty/extra fields...
Could you please point me to the mistake I am making?

Code: Select all

Below some links to topics that you've been active.
    "Κεντρικό topic ευχών"
    http://new.local/viewtopic.php?f=94?&t=182

    "Σούπα"
    http://new.local/viewtopic.php?f=61?&t=365

    ""
    

    ""
    

    ""
    

    ""
    

    ""
    

    ""
    


Below some links to the most active topics of the board.

    "Σούπα"
    http://new.local/viewtopic.php?f=61?&t=365
    "Κεντρικό topic ευχών"
    http://new.local/viewtopic.php?f=94?&t=182
    "Τι πήρα - ήρθε σήμερα [13]"
    http://new.local/viewtopic.php?f=61?&t=5895
    "Τι πήρα - ήρθε σήμερα [14]"
    http://new.local/viewtopic.php?f=61?&t=6318
    "Το ξύρισμα της ημέρας : Μάρτιος 2018"
    http://new.local/viewtopic.php?f=113?&t=6031
    ""
    
    ""
    
    ""
    
    ""
    
    ""
    
    ""
I thought that the problem is because of the <!-- BEGIN --> tag. But when I remove it then the loop does not work at all.

You can see the complete code for the php script in github here.
And the email template is here

Thank you for your help!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4741
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Loop in mail template

Post by kasimi »

Remove all <!-- BEGIN x --> and <!-- END x --> and use

Code: Select all

$messenger->assign_vars([
    'USR_TPC_LIST' => $topic_links,
]);
Twig then loops over the USR_TPC_LIST array with {% for user_topic in USR_TPC_LIST %}.
andreask
Registered User
Posts: 655
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: Loop in mail template

Post by andreask »

Works like a charm! (of course)
But could you please explain why not use assign_block_vars instead?
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4741
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Loop in mail template

Post by kasimi »

assign_block_vars() is called with the elements of the array, not the array itself.

Code: Select all

foreach ($array => $el)
{
    $template->assign_block_vars('blockname', $el);
}
is roughly the same as this:

Code: Select all

$template->assign_vars([
    'blockname' => $array,
]);
The second code only works since Twig is used as template engine.
andreask
Registered User
Posts: 655
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: Loop in mail template

Post by andreask »

Ah! I see. Thanks once again!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
Post Reply

Return to “Extension Writers Discussion”