Created
September 11, 2018 06:19
-
-
Save Wavesonics/d1e65a4c094a04e0a7066d92545e9e4f to your computer and use it in GitHub Desktop.
Route helper with params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="{{route 'user.userForName' 'name: user.userName'}}">User Page</a><br/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RouteHelper(private val router: Router) : BasicValueHelper() | |
{ | |
override fun execute(options: Options?) | |
{ | |
if( options != null ) | |
{ | |
val parameters = options.parameters | |
if( parameters != null && parameters.isNotEmpty() ) | |
{ | |
val routeName = parameters[0] as String | |
val args = mutableMapOf<String,Any>() | |
parameters.drop(1).forEach {argument -> | |
argument as String | |
val parts = argument.split(":") | |
val name = parts[0].trim() | |
val key = parts[1].trim() | |
val value = options.getValue(key) ?: throw IllegalStateException("Could not find value for: $key") | |
args[name] = value | |
} | |
val route = router.uriFor(routeName, args) ?: throw IllegalStateException("Could not find route for name: $routeName") | |
append(options, route) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment