<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>

CC BY-SA 3.0
2015-07-23T17:21:06.810Z
Similarly, `date('U')` will output a Unix timestamp. And if I remember correctly, lowercase `u` outputs microseconds.
Lindsey D
CC BY-SA 3.0
2015-07-23T17:05:24.430Z
2015-07-23T17:05:24.430Z
Marion Newlevant
<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>

CC BY-SA 3.0
2015-07-24T06:57:59.017Z
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.
Steve Holland
CC BY-SA 3.0
2015-07-23T17:40:41.233Z
2015-07-23T17:40:41.233Z
Brad Bell
<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>

CC BY-SA 3.0
2015-07-23T17:23:50.860Z
Do you need an actual GUID, or will something unique on the page suffice?
megatrond
CC BY-SA 3.0
2015-07-24T06:56:40.167Z
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.
Steve Holland
<templating>
Create a GUID in a template
# 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 Marion 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 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 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.  

---

