<p>{{ siteUrl }} is just a simple URL taken from your config file, this is helpful for when you want to use absolute linking to things like style sheets js files, images etc. because it's dynamic you can set it differently depending on the environment and not have to worry about links breaking or putting a localhost link into a live template (which would fail)</p>

<p>The url() is useful for generating absolute links to other internal pages, again without worrying about having to check and update links in your template going from live to development, it takes the path specified and attaches the siteUrl variable so {{ url(path/to/page) }} would output ocalhost/path/to/page or yourdomain.com/path/to/page , depending on your environment and config, hope that helps :)</p>

CC BY-SA 3.0
2014-08-07T23:43:38.800Z
That sorta helps, thanks Alec. Other than just prepending the {{ siteUrl }} value, is there any real difference between the two methods? Any advantages or disadvantages to be wary of?
Lindsey D
CC BY-SA 3.0
2014-08-07T23:46:06.817Z
{{ siteUrl }} actually pulls from your Settings -> General -> **Site URL**, which can accept environment variables like {siteUrl}, or it can be overridden entirely by the `siteUrl` parameter in your config file. (Confusing, I know.) [Brandon wrote a good post about it...](http://craftcms.stackexchange.com/a/921/45)
Lindsey D
CC BY-SA 3.0
2014-08-08T06:01:17.783Z
One thing to keep in mind when using `url()` or `siteUrl` for absolute links to CSS, JS, SVG files etc. is that it most likely causes problems in a multilingual config, as it'll always give you the localized URL. If you need to use absolute links and relative linking is no option, [I recommend](http://craftcms.stackexchange.com/a/1424/125) to use a separate, custom `environmentVariable` for your links. Better name it `baseUrl` for example to prevent confusion.
carlcs
CC BY-SA 3.0
2014-08-07T23:02:28.127Z
2014-08-07T23:02:28.127Z
Alec Ritson
<p>The <code>siteUrl</code> global variable is actually set by calling <code>url()</code> without passing anything into it<sup>1</sup>, so if you're asking about the difference between <code>siteUrl</code> and <code>url()</code> <em>without passing a path to it</em>, you will always get the exact same result. The only difference is that it will be ever so slightly more efficient to use <code>siteUrl</code> since that is a variable that has already been defined for you, whereas Craft will have to go through a bit of logic each time <code>url()</code> is called.</p>

<p>As for where to use which, it's mostly a matter of taste. The only time when <code>siteUrl~"some/path"</code> might differ from <code>url("some/path")</code> is if Craft is not set up to hide index.php in URLS - <code>url()</code> will be smart about how to output URLs with index.php and the path appended after it (either in the query string or path info, depending on the usePathInfp config setting).</p>

<p>There is a greater difference between the two in CP templates, though: in that context <code>url()</code> will automatically add the CP trigger to the path for you.</p>

<p>One last thing worth noting is that <code>url()</code> also provides some arguments for changing the schema (http or https) and appending query string parameters to the URL, whether you pass in a relative site path or a full URL. We've documented those features here: <a href="http://buildwithcraft.com/docs/templating/functions#url">http://buildwithcraft.com/docs/templating/functions#url</a></p>

<p><sup>1</sup> or more precisely, UrlHelper::getUrl(), the PHP function that <code>url()</code> maps to</p>

CC BY-SA 3.0
2014-08-08T07:12:13.917Z
Excellent, thanks for the detailed explanation! My big takeaways from this: (1) `url()` maps to UrlHelper::getUrl(), and (2) `siteUrl` is simply the result of `url()`. With that in mind, I think I'll be switching to `siteUrl` for most cases. Thanks Brandon!
Lindsey D
CC BY-SA 3.0
2014-08-08T02:50:02.720Z
2014-08-08T02:55:23.193Z
2014-08-08T02:55:23.193Z
Brandon Kelly
<p>Both of these things:</p>

<pre><code>1.  {{ siteUrl }}path/to/template
2.  url('path/to/template')
</code></pre>

<p>... will generate this:</p>

<pre><code>http://mywebsite.com/path/to/template
</code></pre>

<p>So I'm wondering, <strong>which method is preferred?</strong> Are there any pros/cons of either technique?</p>

<templating><siteurl>
What's the difference between {{ siteUrl }} and url()?
# What's the difference between {{ siteUrl }} and url()?

(Asked by: username_0 on 2014-08-07)

Both of these things:
1.  {{ siteUrl }}path/to/template
2.  url('path/to/template')

... will generate this:
http://mywebsite.com/path/to/template

So I'm wondering, which method is preferred? Are there any pros/cons of either technique?

**Tags:** templating, siteurl

---

## 2 answers

### Answer by username_3 on 2014-08-08. Score: 12 | Accepted answer

The `siteUrl` global variable is actually set by calling `url()` without passing anything into it1, so if you're asking about the difference between `siteUrl` and `url()` *without passing a path to it*, you will always get the exact same result. The only difference is that it will be ever so slightly more efficient to use `siteUrl` since that is a variable that has already been defined for you, whereas Craft will have to go through a bit of logic each time `url()` is called.

As for where to use which, it's mostly a matter of taste. The only time when `siteUrl~"some/path"` might differ from `url("some/path")` is if Craft is not set up to hide index.php in URLS - `url()` will be smart about how to output URLs with index.php and the path appended after it (either in the query string or path info, depending on the usePathInfp config setting).

There is a greater difference between the two in CP templates, though: in that context `url()` will automatically add the CP trigger to the path for you.

One last thing worth noting is that `url()` also provides some arguments for changing the schema (http or https) and appending query string parameters to the URL, whether you pass in a relative site path or a full URL. We've documented those features here: <http://buildwithcraft.com/docs/templating/functions#url>

1 or more precisely, UrlHelper::getUrl(), the PHP function that `url()` maps to

> **Comments:**
> **username_0**: Excellent, thanks for the detailed explanation! My big takeaways from this: (1) `url()` maps to UrlHelper::getUrl(), and (2) `siteUrl` is simply the result of `url()`. With that in mind, I think I'll be switching to `siteUrl` for most cases. Thanks Brandon!  

---

### Answer by username_1 on 2014-08-07. Score: 2

{{ siteUrl }} is just a simple URL taken from your config file, this is helpful for when you want to use absolute linking to things like style sheets js files, images etc. because it's dynamic you can set it differently depending on the environment and not have to worry about links breaking or putting a localhost link into a live template (which would fail)

The url() is useful for generating absolute links to other internal pages, again without worrying about having to check and update links in your template going from live to development, it takes the path specified and attaches the siteUrl variable so {{ url(path/to/page) }} would output ocalhost/path/to/page or yourdomain.com/path/to/page , depending on your environment and config, hope that helps :)

> **Comments:**
> **username_0**: That sorta helps, thanks Alec. Other than just prepending the {{ siteUrl }} value, is there any real difference between the two methods? Any advantages or disadvantages to be wary of?  
> **username_0**: {{ siteUrl }} actually pulls from your Settings -> General -> **Site URL**, which can accept environment variables like {siteUrl}, or it can be overridden entirely by the `siteUrl` parameter in your config file. (Confusing, I know.) [Brandon wrote a good post about it...](http://craftcms.stackexchange.com/a/921/45)  
> **username_2**: One thing to keep in mind when using `url()` or `siteUrl` for absolute links to CSS, JS, SVG files etc. is that it most likely causes problems in a multilingual config, as it'll always give you the localized URL. If you need to use absolute links and relative linking is no option, [I recommend](http://craftcms.stackexchange.com/a/1424/125) to use a separate, custom `environmentVariable` for your links. Better name it `baseUrl` for example to prevent confusion.  

---

