Answers.Body: <p>If a simple timestamp is unique enough, you can use:</p>
  
  <pre><code>{{ now|date('YmdHis') }}
  </code></pre>
  
  <p>Which will give you something like:</p>
  
  <pre><code>20150723170500
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T17:21:06.810Z
Answers.Text: Similarly, `date('U')` will output a Unix timestamp. And if I remember correctly, lowercase `u` outputs microseconds.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T17:05:24.430Z
Answers.LastActivityDate: 2015-07-23T17:05:24.430Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <p>If you truly need a GUID, then a small plugin that adds it's own <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension" rel="nofollow">Twig extension</a> can add a <code>{{ getUUID() }}</code> method that would look like this:</p>
  
  <pre><code>public function getUUID()
  {
      return StringHelper::UUID();
  }
  </code></pre>
  
  <p>Or for other random strings, <code>{{ getRandomString(12, true) }}</code>:</p>
  
  <pre><code>public function getRandomString($length = 36, $extendedChars = false)
  {
      return StringHelper::randomString($length, $extendedChars);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-24T06:57:59.017Z
Answers.Text: Thanks Brad. In the end I went with a solution similar to what Marion suggested with a combination of `date` and `random` twig filters just for simplicity.
Answers.UserDisplayName: Steve Holland
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T17:40:41.233Z
Answers.LastActivityDate: 2015-07-23T17:40:41.233Z
Answers.UserDisplayName: Brad Bell
Body: <p>Is there a straightforward way of creating a GUID or some other type of unique identifier within a Twig Template?</p>
  
  <p>I'm aware of <a href="http://buildwithcraft.com/classreference/helpers/StringHelper#UUID-detail" rel="nofollow">StringHelper.UUID</a> (mentioned in a different question), but it doesn't look like I can use it directly in a template.</p>
  
  <p>I'm comfortable creating a small plugin to expose this, or alternatively creating a twig filter to use a relevant PHP method, I just didn't want to go re-inventing the wheel if there's already a good solution I don't know about. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-23T17:23:50.860Z
Comments.Text: Do you need an actual GUID, or will something unique on the page suffice?
Comments.UserDisplayName: megatrond
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-24T06:56:40.167Z
Comments.Text: It did need to be something unique, but not necessarily a GUID. I went with  a solution similar to what Marion suggested with a combination of `date` and `random` twig filters.
Comments.UserDisplayName: Steve Holland
Tags: <templating>
Title: Create a GUID in a template
ThreadText: # Create a GUID in a template
  
  (Asked by: username_0 on 2015-07-23)
  
  Is there a straightforward way of creating a GUID or some other type of unique identifier within a Twig Template?
  
  I'm aware of [StringHelper.UUID](http://buildwithcraft.com/classreference/helpers/StringHelper#UUID-detail) (mentioned in a different question), but it doesn't look like I can use it directly in a template.
  
  I'm comfortable creating a small plugin to expose this, or alternatively creating a twig filter to use a relevant PHP method, I just didn't want to go re-inventing the wheel if there's already a good solution I don't know about.
  
  **Tags:** templating
  
  > **Comments:**
  > **username_1**: Do you need an actual GUID, or will something unique on the page suffice?  
  > **username_0**: It did need to be something unique, but not necessarily a GUID. I went with  a solution similar to what username_2 suggested with a combination of `date` and `random` twig filters.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-07-23. Score: 3 | Accepted answer
  
  If a simple timestamp is unique enough, you can use:
  
  ```
  {{ now|date('YmdHis') }}
  
  ```
  
  Which will give you something like:
  
  ```
  20150723170500
  
  ```
  
  > **Comments:**
  > **username_3**: Similarly, `date('U')` will output a Unix timestamp. And if I remember correctly, lowercase `u` outputs microseconds.  
  
  ---
  
  ### Answer by username_4 on 2015-07-23. Score: 1
  
  If you truly need a GUID, then a small plugin that adds it's own [Twig extension](http://buildwithcraft.com/docs/plugins/hooks-reference#addTwigExtension) can add a `{{ getUUID() }}` method that would look like this:
  
  ```
  public function getUUID()
  {
      return StringHelper::UUID();
  }
  
  ```
  
  Or for other random strings, `{{ getRandomString(12, true) }}`:
  
  ```
  public function getRandomString($length = 36, $extendedChars = false)
  {
      return StringHelper::randomString($length, $extendedChars);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_4. In the end I went with a solution similar to what username_2 suggested with a combination of `date` and `random` twig filters just for simplicity.  
  
  ---
  
  