How to make a custom helper for template? [V5] #3453
-
Hello Team, |
Beta Was this translation helpful? Give feedback.
Answered by
Julien-R44
Dec 29, 2021
Replies: 1 comment 1 reply
-
Hello, looks at this : https://docs.adonisjs.com/guides/views/data-flow#globals You have to define your helpers in an preloaded file so :
And select "during HTTP server" Your .adonisrc.json should contains this now : {
"preloads": [
"./start/routes",
"./start/kernel",
{
"file": "./start/ViewHelpers",
"environment": [
"web"
]
}
], Now you can define your helper in start/ViewHelpers : import View from '@ioc:Adonis/Core/View'
View.global('nl2br', function (text) {
return text.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2')
}) In your template : <p> {{{ nl2br(post.description) }}} </p> |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
pk4all
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, looks at this : https://docs.adonisjs.com/guides/views/data-flow#globals
You have to define your helpers in an preloaded file so :
And select "during HTTP server"
Your .adonisrc.json should contains this now :
Now you can define your helper in start/ViewHelpers :
In your template :