I want to create a custom helper, which will get the URL of the current page. I need to get the URL, which is shown in the browser’s address bar, therefore {{url absolute=“true”}} doesn’t suit my needs. How to get the URL in the helper’s code?
Can you say a bit more about the URL you want to return? {{url absolute="true"}}
will return the URL that’s in the browser’s address bar, unless I’m missing something…
I want to create a multi-language blog using private tags (e.g. #es and #fr). Those tags will be added to URL (e.g. example.com/es for Spanish). Ghost should extract the language code from URL and add it to all posts filters in my theme.
Regarding {{url absolute=“true”}}, my Spanish homepage has address http://localhost:2368/es/ in browser, but {{url absolute=“true”}} outputs http://localhost:2368/. So, the language code is missing.
My routes.yaml:
routes:
/es/:
controller: channel
template: home
collections:
/:
permalink: /{slug}/
template: index
taxonomies:
tag: /tag/{slug}/
author: /author/{slug}/
When I set up the routes you have, I get this:
{{url}}
→ /es/
{{url absolute="true"}}
→ http://example.com/es/
Are you sure the /es/ route is working correctly? Does it show the home
template?
Ryan, you are right {{url}} returns the correct full URL in the home template. My mistake was that I tested {{url}} inside a get post block.
I’ve created helper {{lang_code}} that returns language code extracted from URL. But it doesn’t work inside the following block helper (though it returns correct value before the block helper):
{{#get “posts” limit=“4” filter=“tag:{{lang_code}}” include=“tags”}}
Is it possible to execute my helper {{lang_code}} once in “index” or “home” template and put the output in a global variable, which I’ll be able to use in any “{{#get “posts”…” block helper?
Where are you constructing your custom helper? This isn’t possible within the theme.
Another path would be to use custom routing:
Then, you can define your route (/es/) and filter the posts to only show those posts tagged with es
or however you want to do it.