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?

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.

  • My bad, just checked again, and it still gives me a "Scope error". Also tried log.dateFinished is defined and log.dateFinished is not iterable. – Victor In Jul 15 '14 at 23:31
  • What's the actual error you're getting? log.dateFinished is undefined? – Brad Bell Jul 15 '14 at 23:36
  • Craft\Mailer_LogRecord and its behaviors do not have a method or closure named "dateFinished". – Victor In Jul 15 '14 at 23:39
  • OH! Updating my answer. :) – Brad Bell Jul 15 '14 at 23:45
  • 2
    Mailer_LogModel::populateModel($record) – Brad Bell Jul 16 '14 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.