Skip to content

Instantly share code, notes, and snippets.

@serapheem
serapheem / gist:4023912
Created November 6, 2012 10:29
Symfony2 - Generate links in template
Twig:
<a href="{{ path('_welcome') }}">Home</a>
PHP:
<a href="<?php echo $view['router']->generate('_welcome') ?>">Home</a>
If more complicated route.
Twig:
@serapheem
serapheem / gist:4023915
Created November 6, 2012 10:30
Symfony2 - Get Flash message in template
Twig:
{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.flash('notice') }}
</div>
{% endif %}
PHP:
<?php if ($view['session']->hasFlash('notice')): ?>
<div class="flash-notice">
@serapheem
serapheem / gist:4023920
Created November 6, 2012 10:31
Symfony2 - Global Template Variables
List:
app.security - The security context.
app.user - The current user object.
app.request - The request object.
app.session - The session object.
app.environment - The current environment (dev, prod, etc).
app.debug - True if in debug mode. False otherwise.
Twig:
@serapheem
serapheem / gist:4023925
Created November 6, 2012 10:32
Symfony2 - Include template inside another template
Twig:
{# src/Acme/ArticleBundle/Resources/Article/list.html.twig #}
{% extends 'AcmeArticleBundle::layout.html.twig' %}
{% block body %}
<h1>Recent Articles<h1>
{% for article in articles %}
{% include 'AcmeArticleBundle:Article:articleDetails.html.twig' with {'article': article} %}
{% endfor %}
@serapheem
serapheem / gist:4023927
Created November 6, 2012 10:32
Symfony2 - Linking to Assets
Twig:
<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" type="text/css" />
PHP:
<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="Symfony!" />
<link href="<?php echo $view['assets']->getUrl('css/blog.css') ?>" rel="stylesheet" type="text/css" />
@serapheem
serapheem / gist:4023928
Created November 6, 2012 10:33
Symfony2 - Render controller action from template
Twig:
{# app/Resources/views/base.html.twig #}
{# ... #}
<div id="sidebar">
{% render "AcmeArticleBundle:Article:recentArticles" with {'max': 3} %}
</div>
PHP:
@serapheem
serapheem / gist:4023929
Created November 6, 2012 10:33
Symfony2 - Using assetic for asset management
Twig:
{% javascripts '@AcmeFooBundle/Resources/public/js/*' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% stylesheets '@AcmeFooBundle/Resources/public/css/*' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
PHP:
@serapheem
serapheem / gist:4023931
Created November 6, 2012 10:34
Symfony2 - Clear the cache
php app/console cache:clear --env=prod --no-debug
@serapheem
serapheem / gist:4023932
Created November 6, 2012 10:34
Symfony2 - Common doctrine console commands
Create the database:
php app/console doctrine:database:create
Create the entity:
php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --fields="name:string(255) price:float description:text"
Update the entities:
php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product
php app/console doctrine:generate:entities AcmeStoreBundle
php app/console doctrine:generate:entities Acme
@serapheem
serapheem / gist:4023933
Created November 6, 2012 10:35
Symfony2 - Creating a bundle
php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml