Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer
    
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 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 :)

share|improve this answer
    
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 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 at 23:46
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 to use a separate, custom environmentVariable for your links. Better name it baseUrl for example to prevent confusion. –  carlcs Aug 8 at 6:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.