Giter VIP home page Giter VIP logo

Comments (20)

reinink avatar reinink commented on May 21, 2024 1

This has been fixed! See 3688ad5.

from plates.

jelofson avatar jelofson commented on May 21, 2024

+1 on this issue. I have an extension that I want to escape input on, but currently can't without duplication of the escape method.

from plates.

reinink avatar reinink commented on May 21, 2024

Hey gents, I'm a little behind on my Plates tickets due to some recent focus on my other library. However, I'll have a look at this closer in a couple days. Thanks for your patience!

from plates.

elazar avatar elazar commented on May 21, 2024

@reinink A PR for this change should be relatively short and simple. If you want me to submit one, just poke me while we're both at Lone Star PHP.

from plates.

cdCorey avatar cdCorey commented on May 21, 2024

+1 for this. Like @jelofson, I have extensions that need to do escaping, and it would be much better to rely on the built-in escape method rather than duplicating it.

from plates.

reinink avatar reinink commented on May 21, 2024

Sorry for the long delay here folks. I've started a PR for this: #122

My only real concern at this point is what to tag this "fix" as. I'm thinking it could be considered a simple bug fix, however, if anyone has created extensions using these function names, they will stop working and it will use the defaults instead. However, doing a 4.0 release for this seems much too extreme as well.

Any thoughts?

from plates.

elazar avatar elazar commented on May 21, 2024

@reinink

if anyone has created extensions using these function names, they will stop working and it will use the defaults instead

Not sure I follow what you mean. Can you provide a code example that shows this behavior?

from plates.

reinink avatar reinink commented on May 21, 2024

Basically I just mean that if someone created an extension using one of the default functions (ie. escape), then this patch will now switch back to using the Plates function, not the extension function. For example:

class MyEscapeExtension implements \League\Plates\Extension\ExtensionInterface
{
    public $template;

    public function register(\League\Plates\Engine $engine)
    {
        $engine->registerFunction('escape', [$this, 'escape']);
    }

    public function escape()
    {
        // some escape functionality
    }
}

However, maybe this isn't an issue. If someone replaces a default Plates function with their own extension, then maybe a break caused by this "fix" is their own fault?

from plates.

elazar avatar elazar commented on May 21, 2024

@reinink

However, maybe this isn't an issue. If someone replaces a default Plates function with their own extension, then maybe a break caused by this "fix" is their own fault?

I wouldn't put the onus on the user to predict potential future core method names. More than likely, they've added their own functionality that core was lacking at the time, and that sort of innovation should be encouraged.

That said, if there's an easy way to adjust their code to avoid changing its behavior as a result of upgrading to a version with this "fix," that would be preferable, and may not necessarily necessitate a major version bump.

I'm not sure how extension method names are resolved as compared to core method names. Does it have to do with the order in which extensions are loaded? Do extension methods simply take precedence over core methods? Does core lack a way to resolve such conflicts, and maybe a method of doing so needs to be introduced?

from plates.

reinink avatar reinink commented on May 21, 2024

I wouldn't put the onus on the user to predict potential future core method names.

I totally agree. I'm talking about existing core method names.

Does it have to do with the order in which extensions are loaded?

The core methods take priority, and then extension functions are called if those functions do not already exist.

from plates.

elazar avatar elazar commented on May 21, 2024

I totally agree. I'm talking about existing core method names.

Clarification: existing core method names that, as in this case, would end up taking precedence over existing extension method names in the event of a conflict.

The core methods take priority, and then extension functions are called if those functions do not already exist.

Would it be possible to add a method of explicitly overriding core methods with extension methods? I ask that question with some skepticism, as it seems like this change would mean giving users enough rope to hang themselves with. However, it seems worth consideration nonetheless, as it would provide a way for users with conflicts in this scenario to retain any existing behavior of theirs despite the effects of the "fix."

from plates.

jelofson avatar jelofson commented on May 21, 2024

I think the concern is valid if someone did write their own escape extension. That was something I hadn't even considered. All I want to do is the following:

class MyExtension implements \League\Plates\Extension\ExtensionInterface
{
    public $template;

    public function register(\League\Plates\Engine $engine)
    {
        $engine->registerFunction('makeAwesome', [$this, 'awesome']);
    }

    public function awesome($string)
    {
        // Do some stuff with string
        $newString = str_rot13($string);
        // my string needs some escaping so let's leverage the $template's escape method
        // this only works if the Template::escape() method is public.
       return $this->template->escape($newString);
    }
}

I did not think you could even write a custom escaping extension with the name 'escape' that would override the template's escape method. Am I wrong?

from plates.

jelofson avatar jelofson commented on May 21, 2024

Now that I think about it, the example above by @reinink for a custom escaper with the registered name 'escape' would not work because extensions rely on __call(). You could register an extension called 'escape', but it would not ever work inside a template because that method exists in the Template class.

Correct? Maybe I should dig in there a bit before I go spouting off something that might be wrong :)

from plates.

reinink avatar reinink commented on May 21, 2024

I did not think you could even write a custom escaping extension with the name 'escape' that would override the template's escape method. Am I wrong?

With the current design, if you write a custom escaping extension, it would override the default function. However, with this proposed change (#122), it would not override the default function.

So while it would maybe we odd for someone to have done this, it is possible, and it's possible that this switch could break their setup.

from plates.

jelofson avatar jelofson commented on May 21, 2024

With the current design, if you write a custom escaping extension, it would override the default function. However, with this proposed change (#122), it would not override the default function.

Interesting. I just tried the following ($view is an Engine object):

$view->registerFunction('escape', function ($string) {
    return 'your string is ' . $string;
});

And in my template I called $this->escape('whatever'); and the default function is not overridden.

<h1>blah</h1>
<?=$this->escape('some string'); ?>

I also wrote a custom extension with 'escape' as the name and registered it. Again, the default escape method is not overridden. Maybe I don't understand how it can be overridden when calling registered function relies on __call().

Perhaps I am doing something differently. No bid deal. Whatever you decide is fine with me.

from plates.

reinink avatar reinink commented on May 21, 2024

Yeah maybe I better do some additional testing here first. I want to make sure I know what the side affects of this change are. Leave it with me!

from plates.

ragboyjr avatar ragboyjr commented on May 21, 2024

You know, another possible solution is to move escape and batch out of the Template class and make it an extension. This might simplify everything because these funcs really acts more like a plugin than a core feature related to the template anyways.

This would also resolve #96 because batch would be able to see the escape function.

from plates.

elazar avatar elazar commented on May 21, 2024

@ragboyjr

This would also resolve #92 because batch would be able to see the escape function.

Think you meant #39 or #40 instead of #92 ?

from plates.

ragboyjr avatar ragboyjr commented on May 21, 2024

@elazar I think i meant #96, sorry :p.

from plates.

elazar avatar elazar commented on May 21, 2024

Thanks @reinink! :)

from plates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.