1

Is this a bug or is this me ? (I'm a new to craft)

When using the @web alias directly within an attribute in a Twig template, the alias doesn't seem to be resolved to its corresponding URL. Instead, the literal string @web is rendered in the final output.

Steps to Reproduce:

  1. Define the @web alias in general.php:
->aliases([
    '@web' => 'http://craftsite.ddev.site/'
])

2.Use the alias directly in a Twig template's attribute:

<link rel="stylesheet" href="@web/css/app.css">

  1. View the rendered HTML. The output is:
<link rel="stylesheet" href="@web/css/app.css">

The expected output should be:

<link rel="stylesheet" href="http://craftbites.ddev.site/css/app.css">

Workaround:

Resolving the alias to a variable first and then using that variable in the attribute resolves the issue:

{% set baseUrl = alias('@web') %}
<link rel="stylesheet" href="{{ baseUrl }}/css/app.css">

Environment:

  • Craft version: 4.5
  • PHP version: 8.1

1 Answer 1

1

Pretty sure you have to resolve the alias with twig, you could short it down to just:

<link refl="stylesheet" href="{{ alias('@web') }}/css/app.css">

But you also dont need the full url for the css file:

<link refl="stylesheet" href="/css/app.css">

Should work just fine too.

1
  • Thank you so much for pointing out the correct way to use the alias in Twig. I was stuck trying to use @web directly within an attribute, and I totally missed resolving it with {{ alias('@web') }}/css/app.css or just simplifying with the relative /css/app.css. It seems so clear now!
    – arche
    Commented Oct 4, 2023 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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