I'm trying to find ou a way to make this in twig but I can't find a solution..
preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', ucfirst("thisIsAnExample")) ;
The output in php is "This Is An Example"
Is it possible to do the same thing in twig ?
You will need to use a twig extension which will create a custom function for you to use in twig.
For example:
/**
* Convert camel case to a capitalised human readable string.
*
* #param $camelCase string
* #return string
*/
public function camelCaseToString($camelCase)
{
return preg_replace("/([A-Z])/", " $1", ucfirst($camelCase));
}
This should convert thisIsAnExample to This Is An Example.
Related
I got a lot of products, and it can be filter with a lot of different parameters.
So user input search parameter in a form, and then the list is filter by those parameters.
I have try to create a route like this :
/**
* Display a list of product
* #Route("/product/list/{name}/{price_min}/{price_max}/{publish_date}/{supplier_code}", name="product_list")
*/
public function listProduct(){
// ... Check parameters format and then escape special caracters
// ... Display product logic
return $this->render('Product/product_list.html.twig', $array_params_view);
}
I know you can provide optional parameter, but this solution looks really bad to me...
I think there might be another solution.
I have think to use the Request instead of a lot of parameter, but if I do so, I loose the fonctionality of nice and easily readable URL, and maybe it'll be more difficult to manage routing.
I don't know what is the best solution for search functionnality.
If you use route for search into your list, I think you need to read this : link
Query String is a better way for search.
// the query string is '?foo=bar'
$request->query->get('foo');
// returns 'bar'
/**
* Display a list of product
*
* #Route("/list/", name="product_list")
*
* #param Request $request
*
*/
public function listProduct(Request $request)
{
$name = $request->query->get('name');
$price_min = $request->query->get('price_min');
$price_max = $request->query->get('price_max');
$publish_date = $request->query->get('publish_date');
$supplier_code = $request->query->get('supplier_code');
$list_products = $this->getListProducts($name,$price_min,$price_max,$publish_date,$supplier_code);
//Next code
......
}
You would only have to control within the getListProducts function
or whatever you call it, that the arguments can arrive as null
I want some routes to "exists", but to redirect to another route.
For example, this is what I did with the / route
/**
* Homepage exists but redirect to projet
*
* #Route("/", name="homepage")
* #Method("GET")
*/
public function indexAction() {
return $this->redirectToRoute('projet_index');
}
/**
* #Route("/projets/", name="projet_index")
* #Method("GET")
*/
public function indexAction() {}
The thing I want to know is if it's this the best method to do it?
Nope the best way to do that is simple as that:
/**
* #Route("/", name="homepage")
* #Route("/projets/", name="projet_index")
* #Method("GET")
*/
public function indexAction() {
// your code here
}
This is somewhat off-topic, but there's a somewhat dirty trick you can use in a similar scenario, where you want to have several URLs resolving to a single route, using placeholders with requirements and default values:
/**
* #Route("/{path<projets/|>?}", name="projet_index")
*/
public function someAction() {
// ...
}
The way it works is by defining an optional placeholder (with the {...} syntax) called "path" (though you can call it something else). That placeholder is given requirements with the <...> syntax: it can be either "projets/" or the empty string "". Therefore, both the "/" and the "/projets/" URLs match that route, and nothing else, as they're off the form "/{path}" with a path placeholder that matches its requirements.
So far, so good. But there's still one thing we need to do: give it a default value with the ?, otherwise methods like redirectToRoute or the Twig function path will complain that we aren't given them a value for all the placeholders. Note that you could also use "/{path<projets/|>?projets/}" to make the default value URL "/projets/" instead of "/".
I know it's not exactly what OP wanted, but I think it can be a useful trick to know, and someone having a question similar to OP's might find it useful.
Trying to validate if one field is not empty (length > 0) then the length of the field being validated must be a certain length (2 characters). It seems like an "Assert\Expression" might work in this situation but I am having trouble trying to find the length of the properties. It seems like you cannot call php functions within the Expression. The expression documentation mentions functions but maybe I do not understand it... Do I need to register my own function that simply return a strlen(). If so how do you register your own functions? Can someone explain if there is a way to do this, or maybe there is a better way than using Expression that I am overlooking...
/**
* #var string
*
* #ORM\Column(name="plate", type="string", length=10)
*/
private $plate;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=2)
* #Assert\Expression(
* "strlen(this.getPlate()) == 0 or (strlen(this.getPlate()) > 0 and strlen(value) == 2)",
* message="Must be 2 characters"
* )
*/
private $state;
In the above case I get an error The function "strlen" does not exist around position 1
Looks like you will need to register your own function. Have a look at the docs: https://symfony.com/doc/current/components/expression_language/extending.html#registering-functions
There is an example on lowercase, strlen should be very similar.
EDIT:
You can also use a callback validator.
/**
* #Assert\Callback()
*/
public function validateState(ExecutionContextInterface $context)
{
if (!empty($this->plate) && mb_strlen($this->state) !== 2) {
$context->buildViolation('State must be 2 characters long')
->atPath('state')
->addViolation();
}
}
But if you are planning to using this kind of validation in multiple places, you can write and register your own validator.
I have to know what are the codes(ISO codes) of the languages in
Intl::getLanguageBundle()->getLanguageNames($this->name);
Anyone know how to do that?
Update
I discover that the codes are in
"alpha-2 code" and exist a list in the site http://www.nationsonline.org/oneworld/language_code.htm
but I what to see the list of the Intl::getLanguageBundle() to see if it has any different codes
Thanks in advance for any help
From the LanguageBundleInterface class, here's the relevant docblock:
/**
* Returns the names of all known languages.
*
* #param string $displayLocale Optional. The locale to return the names in.
* Defaults to {#link \Locale::getDefault()}.
*
* #return string[] A list of language names indexed by language codes.
*/
public function getLanguageNames($displayLocale = null);
So you just need to use the result like this:
$languages = Intl::getLanguageBundle()->getLanguageNames($this->name);
foreach ($languages as $code => $name) {
/*...*/
}
When writing PHP in Netbeans, as long as I provide my functions and classes with "docblox", Netbeans will show what parameters a function expects, what it will return, and a short description whenever I go to call it somewhere else. There is also the benefit of using it for automatically generated API documentation with a tool such as PHPDocumentor2. Is there any kind of equivalent in R? Currently I'm developing in RStudio.
Example of a docblox in PHP:
/**
* This is the description of some function
*
* #param string $foo A foo string
* #return string Foo string in all caps
*/
function cap_foo($foo) { return strtoupper($foo); }
Is docblox the commented part? If so, check roxygen2