Archive for the ‘Nette’ Category

Tired of template undefined field warnings in PhpStorm?

Tuesday, May 13th, 2014

Code inspection is an awesome feature of PhpStorm. Remember the #gotofail SSL Security Bug? Wouldn’t happen to you with PhpStorm because it warns about unreachable statements. And a lot more.

If however, you get too many warnings, you stop paying attention to them. Especially if you know they’re bogus. One of them is the undefined field warning for template variable assignments in Nette presenter.

Seen this a million times?

Referenced field is not found in subject class.
Note: Check is not performed on objects of type “stdClass” or derived.

(more…)

Human join / implode helper

Wednesday, December 11th, 2013

Did you notice that when humans list items, they use a different glue for the last item?
“Who was there?” “Tom, Bob and Jane.”

Here’s a Nette helper that does the same.

  1.  
  2. public function humanJoin($pieces, $glue = ‘, ‘, $lastGlue = ‘ and ‘)
  3. {
  4.   if (empty($pieces)) {
  5.     return null;
  6.   }
  7.  
  8.   if (count($pieces) == 1) {
  9.     return array_shift($pieces);
  10.   }
  11.  
  12.   $lastItem = array_pop($pieces);
  13.   return join($glue, $pieces) . $lastGlue . $lastItem;
  14. }
  15.  

Use as {$friends|humanJoin} or {$aliens|humanJoin:’, ‘:’ nor ‘}.

Dynamic snippets for repeated forms

Wednesday, April 11th, 2012

At the time of this writing, Nette lacks a good support for dynamic snippets. You can define them wrapped in a static snippet but you can’t invalidate them individually (unless they each belong to a different control, I think). You can only invalidate the static parent snippet which means you receive all its dynamic children snippets in the payload.

If you defined only a few snippets with small amount of data in each then no big deal. If however, you have a pageful of forms, each form containing a snippet that you want updated upon the parent form submission then you have to resort to tricks. For instance, you don’t want a message “Saved” displayed in each row/form in a product administration table.
(more…)

Using NCache to store image thumbnails

Wednesday, November 30th, 2011

There are many ways to handle image thumbnails.  Sites with heavy traffic generate resized copies right after the original image is uploaded. Light traffic sites on the other hand, prefer to wait until the thumbnail is actually requested. Nette offers a few creative ways to handle this:

  1. You can use .htaccess to catch requests for non-existent thumbnails and redirect them to your script.
  2. Following the same theme, you can also use your ErrorPresenter to generate the thumbnail and override the response code (so that it’s not 404).
  3. An unusual solution is to use a helper and generate the thumbnail when rendering your template.
  4. Or simply use a presenter action.

In any of these cases, you need to decide where you’re going to store the thumbnail and how you’re going to delete/update it when the main image changes. This is where you can use the excellent built-in caching aparatus.

(more…)

n:src – the missing twin brother of n:href

Wednesday, November 23rd, 2011

As you have probably guessed by now, I love Nette. The more it hurts when I see the documentation lag behind the quality of the framework. Although I don’t understand why Latte built-in macros don’t include n:src when the oh-so-useful n:href is there my main concern however, is that nowhere in the documentation you’ll find detailed information on how to add it yourself. You’re condemned to endlessly search through forum again and crawl through various outdated subdomains of nette.org.

Here’s my solution to the n:src alias based on a post in the cookbook and the built-in n:href macro. You can also take a peek an another post of mine on how to tweak your forms using macros. As always, hopefully it’ll save you some time.

(more…)