Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Dynamic Named Arguments in Function #656

Open
haducloc opened this issue Jul 25, 2023 · 0 comments
Open

Support Dynamic Named Arguments in Function #656

haducloc opened this issue Jul 25, 2023 · 0 comments

Comments

@haducloc
Copy link

haducloc commented Jul 25, 2023

Requirements: I want to build a function that can produces URL based on MVC action/controller named arguments and list of named parameter arguments:

{{ url( action='index', controller='main', __param1='value1', __param2=value2, __param3=model.property3 ) }}

The issue is: It depends on the context of use, the list of parameter names maybe different -> Need dynamic named parameter argument supported.

My code - The following named argument does not exist .....

    public class UrlTagFunction implements Function {
    
        @Override
        public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
    	String action = (String)args.get("action");
    	String controller = (String)args.get("controller");
    
    	// parameters
    	for (Map.Entry<String, Object> arg : args.entrySet()) {
    	    if (arg.getKey().startsWith("__")) {
    
    		// Got a dynamic parameter
    	    }
    	}
    
    	// Build url from action/controller and dynamic parameters
    	
    	return url;
        }
    
        @Override
        public List<String> getArgumentNames() {
    	
    	// **** ISSUE ***** : The developers don't know list of named parameters in advance.
    	
    	return List.of("action", "controller");
        }
    }

My suggestion:

Remove validating of named arguments from io.pebbletemplates.pebble.node.ArgumentsNode

Remove this code:

          // check if user used an incorrect name
          if (!argumentNames.contains(arg.getName())) {
            throw new PebbleException(null,
                "The following named argument does not exist: " + arg.getName(),
                this.lineNumber, self.getName());
          }

And let the developers validate args inside the Function execute method instead.

FYI: JSP/JSTL supports dynamic attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant