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

Twig Bridge - no functionality to add rule #148

Open
harsain opened this issue Jan 19, 2017 · 6 comments
Open

Twig Bridge - no functionality to add rule #148

harsain opened this issue Jan 19, 2017 · 6 comments

Comments

@harsain
Copy link

harsain commented Jan 19, 2017

As per the documentation, slugify has a method addRule which we can call to add custom rules, same is not available via twig bridge.
We have a use-case where we want to use the filter with a custom filter. So I started looking at the code base and seems it can be done with a small change to the SlugifyExtension. I have got it working on my environment and just wanted to your views before I submit a pull request.
Use-Case:
We have words like "Snacks & Bars", which we want to be converted to "snacks-and-bar". Using Slugify class it can be easily done by using the method addRule('&', 'and'), but we are using it in the twig as a filter.

So the change will be as follows:

public function slugifyFilter($string, $separator = null, $newRules = [])
    {
	    if (! empty($newRules)) {
		    $this->slugify->addRules($newRules);
	    }
	    return $this->slugify->slugify($string, $separator);
    }

Regards,

@florianeckerstorfer
Copy link
Member

You should call addRules() on the Slugify object before you inject it to the Cocur\Slugify\Bridge\Twig\SlugifyExtension constructor.

@harsain
Copy link
Author

harsain commented Feb 10, 2017

That's correct. but will only work if we know the rule to be added.
instead the scenario i am talking about is when the rule is not known and will be passed from the twig template.

So something like:

{{name | slugify(null, {'&': 'and'}) }}

As, you can see here the php level code doesn't know the rule but will be passed from template

Regards,

@nei
Copy link

nei commented Feb 10, 2017

This configuration would also be appreciated on SF bridge.

Something like this or anything that allow us to addRule for simple cases.

cocur_slugify:
    separator: "_"
    rulesets:
        - default
        - [".",""]

@florianeckerstorfer
Copy link
Member

florianeckerstorfer commented Feb 11, 2017

@nei This would be the correct approach for solving this.

When we are talking about just the Twig integration because it could be part of the SF bridge, but it could also mean just straight-up Twig.

Alternatively for Twig we could add a slugfiy_add_rule tag to allow editing rules. I would prefer not to change the call for slugify because I think it should match the PHP methods signature.

@harsain
Copy link
Author

harsain commented Feb 14, 2017

@florianeckerstorfer adding a tag would be a better option ...

I was more talking about the scenario where the Frontend guys don't know anything about the backend and have no knowledge about the configurations, in that scenario they can just pass and new rules directly from the twig template and no change in the backend will be required

@Pazns
Copy link

Pazns commented Jun 17, 2018

@harsain Putting this kind of logic inside your template seems like a good idea to get unmanageable code.

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

No branches or pull requests

4 participants