How to make url slug using LUA?

Ask anything you want to know
Post Reply
leedalgoo
Posts: 11
Joined: Sun Mar 19, 2023 7:18 am

How to make url slug using LUA?

Post 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://dschat.club/viewtopic.php?t= ... 334f5cf23c, it's using format like this

Code: Select all

%title|slug%
but it's still not working
User avatar
francisco
Posts: 58
Joined: Tue Mar 07, 2023 1:48 pm
Location: Brazil
Contact:

Re: How to make url slug using LUA?

Post 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
😴
leedalgoo
Posts: 11
Joined: Sun Mar 19, 2023 7:18 am

Re: How to make url slug using LUA?

Post 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%
User avatar
francisco
Posts: 58
Joined: Tue Mar 07, 2023 1:48 pm
Location: Brazil
Contact:

Re: How to make url slug using LUA?

Post 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>")
😴
Post Reply