Both of these things:

1.  {{ siteUrl }}path/to/template
2.  url('path/to/template')

... will generate this:


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

2 Answers 2


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

  • 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
    Aug 8, 2014 at 7:12

{{ 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 :)

  • 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
    Aug 7, 2014 at 23:43
  • {{ 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...
    – Lindsey D
    Aug 7, 2014 at 23:46
  • 3
    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 to use a separate, custom environmentVariable for your links. Better name it baseUrl for example to prevent confusion.
    – carlcs
    Aug 8, 2014 at 6:01

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.