Skip to content

Loading…

Added UnCamelCase Extension #58

Open
wants to merge 6 commits into from

3 participants

@triplejumper12

I added a filter to convert camel case strings to space separated strings.

ie.
{% set someString = 'someRandomCamelCaseString' %}
{{ someString|unCamelCase }}

outputs: "some random camel case string"

@stof stof commented on an outdated diff
lib/Twig/Extensions/Extension/UnCamelCase.php
((10 lines not shown))
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'unCamelCase';
+ }
+
+ /**
+ * Returns a list of filters to add to the existing list.
+ *
+ * @return array An array of functions
+ */
+ public function getFilters()
+ {
+ return array(
+ 'unCamelCase' => new Twig_Filter_Method($this, 'unCamelCaseString'),
@stof
stof added a note

according to the Twig coding standards, the filter name should use underscores, not camel case.

Good call. I just did camel case by habit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff
lib/Twig/Extensions/Extension/UnCamelCase.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Twig Extension to add the UnCamelCaseFilter
+ */
+class Twig_Extensions_Extension_UnCamelCase extends Twig_Extension
+{
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'un_came_case';
@stof
stof added a note

typo here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof

Btw, you should probably add it in the Text extension instead of adding a new one

@triplejumper12

What is the status of this?

@sstok sstok commented on an outdated diff
lib/Twig/Extensions/Extension/Text.php
@@ -44,6 +45,12 @@ public function getName()
}
}
+function twig_un_camel_case_filter($string)
+{
+ $newString = preg_replace('`([A-Z])`', ' $1', $string);
+ return strtolower($newString);
@sstok
sstok added a note

return must be on its own line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 2 deletions.
  1. +10 −2 lib/Twig/Extensions/Extension/Text.php
View
12 lib/Twig/Extensions/Extension/Text.php
@@ -22,8 +22,9 @@ class Twig_Extensions_Extension_Text extends Twig_Extension
public function getFilters()
{
$filters = array(
- 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
- 'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)),
+ 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
+ 'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)),
+ 'un_camel_case' => new Twig_Filter_Function('twig_un_camel_case_filter'),
);
if (version_compare(Twig_Environment::VERSION, '1.5.0-DEV', '<')) {
@@ -44,6 +45,13 @@ public function getName()
}
}
+function twig_un_camel_case_filter($string)
+{
+ $newString = preg_replace('`([A-Z])`', ' $1', $string);
+
+ return strtolower($newString);
+}
+
function twig_nl2br_filter($value, $sep = '<br />')
{
return str_replace("\n", $sep."\n", $value);
Something went wrong with that request. Please try again.