URLs and paths

URLs in your templates should be created using the {url}, {navigate} or {view} Smarty functions, to ensure compatibility with all possible Thelia configuration.

URLs in your template should always be absolute !

{url}

This Smarty function builds an absolute URL from a route path:

{url path="/contact"}

will gives:

http://www.yourshop.com/contact

path

The value of the path parameter is the route path you want to get as an URL. For example, to get the URL of the /account/password route, use :

<a href="{url path="/account/password"}">{intl l='Change your password'}</a>

Add dynamic parameters in you path

This functionality is only available since version 2.1

In some cases you need to inject dynamic parameter in your path. The parameter uses the same behaviour as the {intl} function. Every %varname found in the string will be replaced by the value of the varname parameter.

Example :

{url path="/product/%id" id=$product_id}

In the previous example, if $product_id value is 1, the {url} will ouput : http://www.yourshop.com/product/1

file

The value of the file parameter is the absolute path (from /web) of a real file, that will be served by your web server, and not processed by Thelia.

For example, if you put a guide.pdf file in the /web directory, the URL of this file is :

{url file="/guide.pdf"}

which gives :

http://www.yourshop.com/guide.pdf

noamp

Setting noamp=1 will escape all & as &amp; that may be present in the generated URL.

router and route_id

This functionality is only available since version 2.3

Since the version 2.3, it’s possible to generate an URL from the route id. The argument router has a default value the current environment (front or admin).

{url route_id="contact.success"}
{url route_id="admin.catalog"}
{url route_id="admin.folders.update" folder_id=42}

Example for a module :

{url router="paypal" route_id="paypal.configure"}

Adding custom parameters to the generated URL

You may add as many parameters as you want to the generated URL, by adding them as {url} parameters :

{url path="/contact" myvar="1" myothervar="2"}

will gives :

http://www.yourshop.com/contact?myvar=1&myothervar=2

{viewurl}

This function generates the absolute URL to a front office view (e.g. an HTML file in the current template). If we have for example a legal.html file in a front-office template, the way to get the URL to display this view is:

<a href="{viewurl view="legal"}">{intl l="Legal"}</a>

which gives :

<a href="http://www.yourshop.com/?view=legal">{intl l="Legal"}</a>

You may also add as many parameters as you want to the generated URL, by adding them as {viewurl} parameters :

{viewurl view="legal" myvar="1" myothervar="2"}

will gives :

http://www.yourshop.com/?view=legal&myvar=1&myothervar=2

{admin_viewurl}

{admin_viewurl} performs the same URL generation as {viewurl}, but operates on back office templates only.

Please refer to {viewurl} for usage and details.

The {navigate}function is a convenient way to generate URLs pointing to common locations. This function has only one parameter, to, which may take one of the following values:

  • current: this is the absolute URL of the current page
  • previous : this is the absolute URL of the previous page
  • index : this is the absolute URL of the shop home page

Example:

  • return to the shop home page : <a href="{navigate to='index'}">{intl l="Back to home"}</a>
  • go back to the previous page : <a href="{navigate to='previous'}">{intl l="Back to home"}</a>
  • reload the current page : <a href="{navigate to='current'}">{intl l="Reload !"}</a>

You can’t add custom parameters to the URL generated by {navigate}. To do so, use is along with the {url} function :

{url path={navigate to="current"} limit="4"}

This way the limit=4 parameter is added to the URL : http://www.myshop.com/current-page-url.html?limit=4

{set_previous_url}

In most cases, Thelia automatically sets the previous URLs, so that {navigate to='previous'} will generate the URL to the previous page visited by your customer. But in some cases, you may want to define yourself the URL of the previous page, so that your customers will go back to this specific page instead of the page they have visited before.

The {set_previous_url} function allows setting the URLs of the page thant will become the previous page :

{set_previous_url path='path/.to/some/page'}

To get possible {set_previous_url} parameters, please see the {url} function.

You may also want to prevent a page to become the “previous page”. To do so, use the ignore_current parameter, and put this call somewhere in the page :

{set_previous_url ignore_current=1}

A typical example is the login and/or register page, where the user should go back where it was before entering its credentials (cart or order page), and not te register/login page itself.

If your page extends another one (typically layout.tpl), be sure to put the call inside a {block}...{/block}, otherwise it wouldn’t be executed.