Answers.Body: <p>You shouldn't be returning a <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow">record</a> instance to your templates directly. That can lead to all sorts of badness like templates being able to update the database themselves.</p>
  
  <p>Have your plugin's <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">service</a> populate a <a href="http://buildwithcraft.com/docs/plugins/models" rel="nofollow">Mailer_LogModel</a> with just the attributes you want to expose to your template and return that.  Pretty sure after you do that, a simple:</p>
  
  <pre><code>{% if log.dateFinished %}
  </code></pre>
  
  <p>will work.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T23:31:52.617Z
Answers.Text: 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`.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T23:36:18.077Z
Answers.Text: What's the actual error you're getting?  log.dateFinished is undefined?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T23:39:32.117Z
Answers.Text: `Craft\Mailer_LogRecord and its behaviors do not have a method or closure named "dateFinished".`
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T23:45:39.490Z
Answers.Text: OH!  Updating my answer. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-16T00:03:31.967Z
Answers.Text: Makes sense, thanks. Noob question: Is there a Yii-shortcut which can populate the model? (I need all attributes)
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-16T00:05:48.797Z
Answers.Text: Mailer_LogModel::populateModel($record)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-16T17:08:16.910Z
Answers.Text: Note: For some reason when populating a Record, `model()` needs to be added: `Mailer_LogRecord::model()->populateRecord($log)`
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T23:23:02.667Z
Answers.LastActivityDate: 2014-07-16T00:01:06.960Z
Answers.LastEditDate: 2014-07-16T00:01:06.960Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a optional <code>dateFinished</code> attribute in my <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow noreferrer">Record</a>:</p>
  
  <pre class="lang-php prettyprint-override"><code>protected function defineAttributes()
  {
      return array(
          ...
          'dateFinished'  =&gt; AttributeType::DateTime,
          ...
      );
  }
  </code></pre>
  
  <p>Some Records have it set to a <a href="http://php.net/manual/en/datetime.format.php" rel="nofollow noreferrer">DateTime</a> other have it set to <code>null</code> (default).</p>
  
  <p>To prevent Twig errors I tried using the <a href="https://craftcms.stackexchange.com/q/356/177"><code>is defined</code> test</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ log.dateFinished is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}
  </code></pre>
  
  <p>But this doesn't work, I'm getting the typical "Scope error". After taking a look at the <a href="http://twig.sensiolabs.org/doc/functions/dump.html" rel="nofollow noreferrer"><code>dump()</code></a> it looks that even a <code>null</code> value is getting defined:</p>
  
  <pre class="lang-php prettyprint-override"><code>  ["dateFinished"]=&gt;
    object(Craft\DateTime)#2670 (3) {
      ["date"]=&gt;
      string(19) "2014-07-15 17:48:25"
      ["timezone_type"]=&gt;
      int(1)
      ["timezone"]=&gt;
      string(6) "+00:00"
    }
  
    ....
  
    ["dateFinished"]=&gt;
    array(2) {
      ["column"]=&gt;
      string(8) "datetime"
      ["type"]=&gt;
      string(8) "datetime"
    }
  </code></pre>
  
  <p>I tried to solve this by "<a href="https://stackoverflow.com/a/14482336/3589841">Check(ing) if a variable is a date with Twig</a>":</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ log.dateFinished is defined and log.dateFinished.timestamp is defined ? log.dateFinished|date('m.d.y - H:m'|t) : '-'}}
  </code></pre>
  
  <p>But this doesn't make a difference, I'm still getting the "Scope error". Any ideas?</p>
  
Tags: <plugin-development><templating><records>
Title: How to check if a DateTime attribute is set?
ThreadText: # How to check if a DateTime attribute is set?
  
  (Asked by: username_0 on 2014-07-15)
  
  I have a optional `dateFinished` attribute in my [Record](http://buildwithcraft.com/docs/plugins/records):
  
  ```php
  protected function defineAttributes()
  {
      return array(
          ...
          'dateFinished'  => AttributeType::DateTime,
          ...
      );
  }
  
  ```
  
  Some Records have it set to a [DateTime](http://php.net/manual/en/datetime.format.php) other have it set to `null` (default).
  
  To prevent Twig errors I tried using the [`is defined` test](https://craftcms.stackexchange.com/q/356/177):
  
  ```twig
  {{ 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()`](http://twig.sensiolabs.org/doc/functions/dump.html) it looks that even a `null` value is getting defined:
  
  ```php
    ["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](https://stackoverflow.com/a/14482336/3589841)":
  
  ```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?
  
  **Tags:** plugin-development, templating, records
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-07-15. Score: 4 | Accepted answer
  
  You shouldn't be returning a [record](http://buildwithcraft.com/docs/plugins/records) 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](http://buildwithcraft.com/docs/plugins/services) populate a [Mailer\_LogModel](http://buildwithcraft.com/docs/plugins/models) 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.
  
  > **Comments:**
  > **username_0**: 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`.  
  > **username_1**: What's the actual error you're getting?  log.dateFinished is undefined?  
  > **username_0**: `Craft\Mailer_LogRecord and its behaviors do not have a method or closure named "dateFinished".`  
  > **username_1**: OH!  Updating my answer. :)  
  > **username_0**: Makes sense, thanks. Noob question: Is there a Yii-shortcut which can populate the model? (I need all attributes)  
  > **username_1**: Mailer_LogModel::populateModel($record)  
  > **username_0**: Note: For some reason when populating a Record, `model()` needs to be added: `Mailer_LogRecord::model()->populateRecord($log)`  
  
  ---
  
  