Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

David Grudl dg

🏠
Working from home
Block or report user

Report or block dg

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dg
dg / websocket.html
Created Aug 11, 2013
WebSocket communication between PHP and single client
View websocket.html
<!doctype html>
<script>
if ("WebSocket" in window) {
var ws = new WebSocket("ws://127.0.0.1:31339");
ws.onopen = function() {
console.log('connected');
};
ws.onerror = function(e) {
@dg
dg / composing.presenters.php
Created May 31, 2018
Composing presenters without inheritance
View composing.presenters.php
<?php
// composing presenters without inheritance
// (requires nette/application 2.4.11)
trait RequireLoggedUser
{
public function injectRequireLoggedUser()
{
@dg
dg / template.latte
Created Aug 26, 2011
Výhody šablonovacího systému Latte
View template.latte

Zadáním je jednoduchý a zcela běžný úkol:

  • vypisuju nebufferované data z databáze (tj. neznám dopředu počet položek) jako elementy
  • každý lichý bude mít class="lichy"
  • poslední bude mít třídu class="posledni" (tedy class="posledni lichy", bude-li poslední zároveň lichý)
  • protože jsme puntičkáři, nechceme v kódu žádné prázdné
  • apod.
  • a samozřejmě veškerý výstup musí být escapovaný (tj. ošetřený)
@dg
dg / verifyIC.php
Created Jun 30, 2015
Ověření rodného čísla a IČ
View verifyIC.php
<?php
function verifyIC($ic)
{
// be liberal in what you receive
$ic = preg_replace('#\s+#', '', $ic);
// má požadovaný tvar?
if (!preg_match('#^\d{8}$#', $ic)) {
return FALSE;
@dg
dg / Nette
Created Aug 19, 2014
DI: Symfony versus Nette
View Nette
services:
newsletter_factory: NewsletterFactory
newsletter_manager: @newsletter_factory::get(@templating)
@dg
dg / gist:1009307
Created Jun 5, 2011
Routing in Django verus Nette Framework
View gist:1009307

DJANGO

In urls.py

# urls like "articles/2011/tutorial03" or "articles/2011/tutorial03.html" or "articles/2011/tutorial03.htm"

urlpatterns = patterns('',
    (r'articles/(?P<year>\d+)/(?P<item>[^/]+)(?:\.htm(?:l)?)?/?\$', 'articles.detail'),
)
@dg
dg / persistent.parameter.trait.php
Created May 31, 2018
Transfer of a persistent parameter between two presenters without an inheritance
View persistent.parameter.trait.php
<?php
// transfer of a persistent parameter between two presenters without an inheritance
// (requires nette/application 3.0.0)
trait LangParameter
{
/** @persistent */
public $lang;
@dg
dg / StrictObject.php
Created Jun 11, 2018
Nette\StrictObject, simplified alternative for Nette\SmartObject (compatible with nette/utils 2.4)
View StrictObject.php
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette;
use Nette\Utils\ObjectHelpers;
@dg
dg / typehints.php
Last active May 31, 2018
Converts @param & @return to native PHP typehints
View typehints.php
<?php
declare(strict_types=1);
require '/nette.phar';
class TypeHints
{
public $php71 = true;
@dg
dg / gist:92b041f0315a5fe5745819b180bf7237
Created Apr 20, 2017
Chrome DevTools failing example
View gist:92b041f0315a5fe5745819b180bf7237
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<div style="width: 300px; height: 300px; background: blue"></div>
You can’t perform that action at this time.