M
M
magary42016-12-24 16:32:42
symfony
magary4, 2016-12-24 16:32:42

Defening parameters passed to twig?

in my project, the templates acquire a uniform look ;
several variables must be passed from the controller to the template without fail, for example
, nodeId, metaTags ,
the content variable of the Namespace\MySystem\Content type may or may not be
many pages are lists (products, cart, orders) require the presence in the PagerFanta template
how to make a listener that will check if all variables are passed and if they are of the correct type when trying to call $template->render()?
And along the way, the question is so as not to create a separate topic
if a class with a method is allowed getProducts in case of zero search results, what should Feng Shui return an empty array, null, false or exception?
and what should a method like findOnePost($id) return if not found - null, false or exception?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
amorphine, 2016-12-24
@amorphine

>>how to make a listener that will check if all variables are passed and if they are of the correct type when trying to call $template->render()?
the specificity of php is such that in incomprehensible situations it will sneeze, properditsya, but it will work. Use this fact: twig does not crash when rendering uninitialized "variables" *they are just elements of the data array. In general, it is considered good practice to initialize with nulls at the beginning of the code or create an annotation with the variables used.
About types - IMHO, no need to shoot yourself in the foot and feed the template engine with types. Let everything work on arrays, as far as possible, it is important to simply respect the existence of the necessary keys.
>> what should feng shui return an empty array, null, false or exception?
Exception is a rather slow mechanism, you don't need to use it. null, false, 0 - it doesn't matter. Usually, when the code is waiting for data from a method, they put
And what's there - null, false, 0 doesn't f*ck.

O
OnYourLips, 2016-12-24
@OnYourLips

if a class with a method is allowed getProducts in case of null search results, what should Feng Shui return an empty array, null, false or exception?
Empty collection.
and what should a method like find OnePost($id) return if not found - null, false or exception?
null.
And if get OnePost($id), then exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question