responsiv / campaign-plugin

[PREMIUM] Send professional campaign messages to your subscribers.
http://octobercms.com/plugin/responsiv-campaign
5 stars 2 forks source link

How to use variables inside mail template #38

Closed Fl0Cri closed 7 years ago

Fl0Cri commented 7 years ago

I am trying to make a condition in the template of a newsletter: there is an mediafinder field in my layout that can be set or not. If it is set, I want to display an image inside a <div> (in fact it's a <tr> but i'll use a div in the example) with its src set to the value of the mediafinder. But if the image is not provided, I don't want to render the <div> at all.

In a normal october layout, here's what I would have done:

{variable name="image" type="mediafinder" mode="image" label="Image"}{/variable}

...

{% if image %}
<div>
    <img src="{{ image | media }}" />
</div>
{% endif %}

But it doesn't work: the div is not displayed, even if the image is set.

If I try to do a {{ dump(image) }} I get NULL and if I try {{ dump() }} I can find no trace of my variable, I also tried with other types of variables, it is the same result: variable seems to not be attached to the scope. On the other and, I tried to use a twig {% set image="test_image.png" %} and here I can see it in {{ dump() }}.

Is there something I missed or are variables simply not supported? If so, is there a workaround to solve my initial problem (conditionnally display a block based on the content of a field placed inside)?

daftspunk commented 7 years ago

At first glance, what you have presented should work. I will need to see if I can replicate the issue and get back to you. However, I won't be able to look at this for a few days as I am on vacation from today. Thanks for your patience.

kgirzadas commented 7 years ago

Same issue. Just checked template which is returned from parser and twig variables are missing here.

daftspunk commented 7 years ago

Yes I see now, while {variable} has loose support. The template is rendered using the Bracket parser which doesn't support if statements.

However, it does support some basic filters. You'll notice this works:

{variable name="image" type="mediafinder" mode="image" label="Image"}{/variable}

<div>
    <img src="{image|media}" />
</div>

Keep in mind the template is rendered twice:

  1. Once by the Twig parser to get a static copy of the template (include blog posts, etc)
  2. Second time by the Bracket parser to introduce user-specific values
Fl0Cri commented 7 years ago

Thank you @daftspunk for the explanation about the rendering method, I wasn't aware of this.

In case it helps someone and to answer my initial condition problem, I found (looking into the sources of October\Rain\Parse\Bracket) that there is a parseKeyBooleans method that can parse conditions allowing to do something like this:

{variable name="image" type="mediafinder" mode="image" label="Image"}{/variable}

{?image}
<div>
    <img src="{image|media}" />
</div>
{/image}

However, it doesn't work inside repeaters. I didn't said it in my initial example because I thought it was not necessary, but now my less simplified mail template looks like this:

{repeater name="blocks" prompt="Add a block"}
    <div>
        {variable name="image" type="mediafinder" mode="image" label="Image"}{/variable}
        {?image}
            <div>
                <img src="{image|media}" />
            </div>
        {/image}
        <div>
            {richeditor name="content" label="Content"}Default text{/richeditor}
        </div>
    </div>
{/repeater}

I know it is an October issue, but I think it would be intresting to solve the problem here because it's the only way (at least the only that I could find) to make a condition in a mail template. Also, don't haesitate to tell me if what I did above is absolutely not recomended (it is marked as experimental in the comments).

From what I saw in the Bracket parser code, this line: https://github.com/octobercms/library/blob/master/src/Parse/Bracket.php#L65 should also be added here https://github.com/octobercms/library/blob/master/src/Parse/Bracket.php#L166. I have not tested it at all and I don't know if there is a reason why it has not been done, @daftspunk you probably know it better than me. Tell me if you want a PR on the octobercms/library repo.

developerabdd commented 6 years ago

I am also facing this issue and after so long (more than a year) it would be nice to have some kind of answer about this.