Search code examples
drupaldrupal-theming

Print path to base in template override


I am doing a template override with my template.php, and I need to print the path to the base of my site.

Currently I have this:

$output = '<div id="my-basket-text"><a href="/cart">My Basket</a></div>' . '<div id="my-basket-no-items">' . $item_count . '</div>';

The link created is:

<a href="/cart">My Basket </a>

I understand why this is happening and why this link doesn't work.

If I was adding code to a tpl file, I would use PHP to print the path to the base of my site first. However using the same code in my template.php doesn't work; the code is shown as text on the page.

How can I get around this? Am I trying to do it in the right way?

NOTE - The code $item_count works fine as it is, but when I tried to use it to add a class to a div it also just printed the text: '' . $item_count . ''.

This makes me wonder if I am using the correct code for the base path, but applying it wrongly.


Solution

  • You need to use drupal's link function l(). Check this page for the url's options available.

    So, your link should be

    $output = l('My Basket', 'cart');
    

    If you use that for your links, Drupal will handle the base path for you.