3

I have a optional dateFinished attribute in my Record:

protected function defineAttributes()
{
    return array(
        ...
        'dateFinished'  => AttributeType::DateTime,
        ...
    );
}

Some Records have it set to a DateTime other have it set to null (default).

To prevent Twig errors I tried using the is defined test:

{{ log.dateFinished is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}

But this doesn't work, I'm getting the typical "Scope error". After taking a look at the dump() it looks that even a null value is getting defined:

  ["dateFinished"]=>
  object(Craft\DateTime)#2670 (3) {
    ["date"]=>
    string(19) "2014-07-15 17:48:25"
    ["timezone_type"]=>
    int(1)
    ["timezone"]=>
    string(6) "+00:00"
  }

  ....

  ["dateFinished"]=>
  array(2) {
    ["column"]=>
    string(8) "datetime"
    ["type"]=>
    string(8) "datetime"
  }

I tried to solve this by "Check(ing) if a variable is a date with Twig":

{{ log.dateFinished is defined and log.dateFinished.timestamp is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}

But this doesn't make a difference, I'm still getting the "Scope error". Any ideas?

Victor
  • 8,376
  • 1
  • 34
  • 61

1 Answers1

4

You shouldn't be returning a record instance to your templates directly. That can lead to all sorts of badness like templates being able to update the database themselves.

Have your plugin's service populate a Mailer_LogModel with just the attributes you want to expose to your template and return that. Pretty sure after you do that, a simple:

{% if log.dateFinished %}

will work.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143