Page 1 of 1

How to make url slug using LUA?

Posted: Sat Feb 24, 2024 2:26 am
by leedalgoo
I know there is url.slug() function and i know how to use it too.
I will give you the example of my problem cause i don't know how to explain it

Code: Select all

local param_post = {
limit = 10,
order = "id_desc"
}

local is_ok, postlist = api.post_info(param_post)
local post_index_html = [=[
blah blah
<a href="/post/%id%/%title%>%title%</a>
blah blah
]=]

print(html.render_tag(post_index_html, postlist, true));
i can't put any lua function inside [=[something]=] cause it's escaping every lua function inside it. I want to make my url post become more seo friendly by adding something like

Code: Select all

<a href="/post/%id%/url.slug(%title%)">
do you guys have any idea how to achieve this?

Edit : I have follow this official post http://blog.wapka.xyz/viewtopic.php?t= ... 334f5cf23c, it's using format like this

Code: Select all

%title|slug%
but it's still not working

Re: How to make url slug using LUA?

Posted: Sat Feb 24, 2024 5:07 am
by francisco
Hello! Please try this:

Code: Select all

%title|url.slug%
To explain further, this is as if the return of %title% is used as a parameter for the url.slug(param) method.
This means that you could also do, for example, %title|string.upper% to make the text uppercase, or %title|hash.md5% to generate an MD5 hash from the title, or even use %title|url.redirect%, which I don't recommend 😅

"Ah cool, can I chain the functions too?"
Yes of course! %title|string.upper|url.encode%

Here is a list of the Lua global methods and functions available in Wapka: https://wk.franciscodaschagas.dev/lua-globals

Re: How to make url slug using LUA?

Posted: Sat Feb 24, 2024 6:43 am
by leedalgoo
Thank you so much :o , so the keypoint is we can use lua function inside the double brackets by using this delimiter

Code: Select all

%tag|function%

Re: How to make url slug using LUA?

Posted: Sat Feb 24, 2024 6:59 am
by francisco
leedalgoo wrote: ↑Sat Feb 24, 2024 6:43 am Thank you so much :o , so the keypoint is we can use lua function inside the double brackets by using this delimiter

Code: Select all

%tag|function%
Correct. Any function or method that takes a single parameter can be used in this way. You can even create your own function!

Code: Select all

function AwesomeTitle(title)
    local text = "My awesome title is: " .. title
    return text
end


local param_post = {
limit = 10,
order = "id_desc"
}

local is_ok, postlist = api.post_info(param_post)
local post_index_html = [=[
<li><a href="/post/%id%/%title|url.slug%">%title|AwesomeTitle%</a></li>
]=]
print("<ul>")
print(html.render_tag(post_index_html, postlist, true))
print("</ul>")