Answers.Body: <p>Use the <code>trim</code> filter to automatically remove leading and trailing slashes like so:</p>
  
  <pre><code>{{ url( entry.myField | trim('/') )  }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T07:05:23.100Z
Answers.LastActivityDate: 2015-07-23T07:05:23.100Z
Answers.UserDisplayName: Alex Roper
Body: <p><a href="http://buildwithcraft.com/docs/templating/functions#url" rel="nofollow">The <code>url()</code> function</a> won't work with a leading slash as it's path parameter. </p>
  
  <p>For example, this: <code>{{ url('jobs') }}</code><br>
  Outputs: <code>http://example.com/jobs</code></p>
  
  <p>But this: <code>{{ url('/jobs') }}</code><br>
  Just outputs: <code>/jobs</code></p>
  
  <p>This is easy enough to remember for developers, but if you're passing a variable from a user input field like <code>{{ url(entry.myField) }}</code>, users can accidentally break a link if they add a leading slash.</p>
  
  <p>How can I make the <code>url()</code> function more resilient when relying on an unknown variable?</p>
  
Tags: <templating>
Title: How do I use a leading slash with the url() function?
ThreadText: # How do I use a leading slash with the url() function?
  
  (Asked by: username_0 on 2015-07-23)
  
  [The `url()` function](http://buildwithcraft.com/docs/templating/functions#url) won't work with a leading slash as it's path parameter.
  
  For example, this: `{{ url('jobs') }}`  
  Outputs: `http://example.com/jobs`
  
  But this: `{{ url('/jobs') }}`  
  Just outputs: `/jobs`
  
  This is easy enough to remember for developers, but if you're passing a variable from a user input field like `{{ url(entry.myField) }}`, users can accidentally break a link if they add a leading slash.
  
  How can I make the `url()` function more resilient when relying on an unknown variable?
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-07-23. Score: 4 | Accepted answer
  
  Use the `trim` filter to automatically remove leading and trailing slashes like so:
  
  ```
  {{ url( entry.myField | trim('/') )  }}
  
  ```
  
  
  ---
  
  