In the default templates there are conditions like !sender_name==!sender_name. What is their meaning? Why do they not always return true?

Comments

salvis’s picture

Here's the information from README.txt:

Mail Templates
--------------

Go to admin/build/mail_edit to edit the mail templates.

Some of the variables are only available if you save the templates, 
and not at all in digest mode.

However, as long as you DON'T save the templates, they are automatically
translated to the recipient's language. Like other user-modifiable strings, 
when you edit and save them, they stop getting localized. For multi-language
customization, you need to edit the subscriptions_mail.templates.inc file.

You can use conditional text in the templates. The syntax is

   {{!varname==value?then_text:else_text}} or
   {{!varname!=value?then_text:else_text}}

!varname can be the any variable and will be replaced;
value is a string that doesn't contain a '?';
then_text is a string that doesn't contain a ':' and
else_text doesn't have that limitation.

Both then_text and else_text can contain newlines and variables.

Example:
This {{!has_new_comments==1?has:doesn't have any}} new comments.

The left side of !sender_name==!sender_name is replaced, but the right side isn't. The condition is true only when the variable is not set.

This is how variable replacement works everywhere in Drupal, but it might be worth to add this bit here as a reminder.

gustav’s picture

Status: Active » Fixed

Thanks a lot for that prompt reply. I have added the following text to the help file at http://drupal.org/node/344021:

Note that only variable names that appear on the left-hand side of an equation are replaced and only if the variable is not empty. You can use a condition like !sender_name==!sender_name. The left side of !sender_name==!sender_name is replaced if the sender name is set, but the right side isn't. Therefore the condition is true only when the variable is not set.

Did I get that right?

salvis’s picture

What help file are you referring to? The link points to this thread.

Since the syntax is !varname==value, much of what you write is redundant.

"Not empty" is not the same as "not defined". A variable could be defined and empty, in which case {{!varname==?... would be true.

How about adding this to README.txt:

Undefined variables are replaced by themselves, i.e. they remain as !varname.
Example:
{{!sender_name==!sender_name?Sender is undefined:Sender: !sender_name}}

Variables can be defined and empty. Example:
{{!sender_name==?Sender is empty:Sender is either undefined or non-empty}}
gustav’s picture

Status: Fixed » Active

Ah, I put the wrong link. The correct link to the help page is http://drupal.org/node/344030.

Thanks for making me aware of the distinction between empty and undefined variables. Unfortunately this will not help the end-user because they have no way of telling under which circumstances a variable will be empty and under which circumstances it will be undefined. Most of the theme templates decide on whether to print a particular element based on whether the corresponding variable is empty or not, rather than on whether it is defined or not. I think it would be better if the subscriptions module could follow that convention as well.

For the time being, how would you write a condition that tests for whether a variable is defined and non-empty?

salvis’s picture

You are right, there's no way to test that condition. But does that condition need testing?

We're not addressing the end user here but the administrator, and what we're doing is happening before theming. Take !sender_name, for example: it's either undefined or non-empty. Take !body: it's either empty or not, but never undefined (I think). For some fields I've provided predicates (helper variables that are either 0 or 1).

It will take some experimenting to get the templates just right, but I think we've covered all the bases.

The replacement algorithm is the one being used by t(), btw.

gustav’s picture

Category: support » feature
Priority: Normal » Minor

!body is a good example. Consider an administrator who knows that they have some content types without a body field and who wants to write a node_tid template that can deal with this apropriately. How can we expect this administrator to know whether a content type without a body will have an empty body or an undefined body? You think it will be empty rather than undefined. But that is because you know a lot about the Drupal code, which the administrator can not be expected to do.

Rather than providing more explanations for the administrator about how each variable handles this issue of empty versus undefined, would it not be easy to change the substitution code so that it will substitute the empty string for variables that are either empty OR undefined? That, after all, is what Drupal does with the variables it passes to theme templates, so why not also do it for mail templates?

salvis’s picture

I'm (still) not convinced. No one will get a template right with their first shot. It will always take some experimenting. I don't think this ought to be documented in more detail. Just let the administrator find out what works best for them.

We have three states: undefined, empty, and non-empty. Presently, we can test for
-- undefined (or not)
-- empty (or not)
but we cannot test for non-empty.

You're suggesting to treat undefined values as empty, but this would mean we lose information.

And it would mean we'd have to implement our own token replacement algorithm, which would remove every occurrence of an exclamation mark followed by any number of non-blank characters, unless it was matched by a variable.

gustav’s picture

Category: feature » support
Status: Active » Fixed

Salvis, what you say about the admin just having to experiment sounds sensible. I agree with you that in practice that will probably work fine. Many thanks for all the thought you have put into this.

salvis’s picture

Ok, my pleasure — thanks for a good discussion!

Good night...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.