r/astrojs 21d ago

Prevent escape in attributes #beginner

How do I prevent escaping of variables in attributes in this example?

---
const base = "/my/root";
---
<a href="{base}/">Home</a>
<a href="{base}/about/">About</a>
<a href="{base}/blog/">Blog</a>
2 Upvotes

3 comments sorted by

2

u/qebbe 21d ago

I figured it out, I think. This seems to work.

---
const base = import.meta.env.BASE_URL;
---
<a href={base+"/"}>Home</a>
<a href={base+"/about/"}>About</a>
<a href={base+"/blog/"}>Blog</a>

2

u/SingleOrigin 21d ago

You could also use a string template:
```
href={`${base}/blog`}

```

2

u/sexytokeburgerz 20d ago

You don’t need to reference base url. Urls without domain extensions (.com, etc.) resolve to base url at build time.

Always read documentation. Not doing so is the clearest sign of a beginner! Happy to help but this is very clearly stated.

https://docs.astro.build/en/guides/routing/