Use Symfony2 components inside WordPress

  • 5,920 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,920
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
11

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Use Symfony2 components inside WordPress (…and live happily)
  • 2. Walter Dal Mut  Cofounder at Corley S.r.l.  First level degree Electronic Engineering  Ungraduated student of Computer ScienceMaurizio Pelizzone  Cofounfer at Mavida S.n.c  WordPress Lover
  • 3. Symfony2 is a reusable set of standalone,decoupled, and cohesive PHPcomponents that solve common webdevelopment problemsFabien Potencierhttp://fabien.potencier.org/article/49/what-is-symfony2
  • 4. Use Composer autoload in your theme
  • 5. WordPress & Monolog
  • 6. Monolog is a logging library for PHP 5.3used by Symfony2. It is inspired by thePython LogBook library.
  • 7. Get MonologWebsite: https://github.com/Seldaek/monologUsing Monolog:Create a new composer.json file{ "require": { "monolog/monolog": "1.1.*" }}curl -s http://getcomposer.org/installer | phpphp composer.phar install
  • 8. wrap inside WordPress…
  • 9. … and use in your theme
  • 10. WordPress & Assetic
  • 11. Assetic is an asset managementframework for PHP.Assetic is based on the Python webassetslibrary
  • 12. Get AsseticWebsite: https://github.com/kriswallsmith/asseticUsing Assetic:Create a new composer.json file{ "require": { "kriswallsmith/assetic": "v1.0.4" }}curl -s http://getcomposer.org/installer | phpphp composer.phar install
  • 13. GOAL: Merge, Minimize and Compile alljavascript loaded through WordPress
  • 14. Code example:https://github.com/miziomon/symfonyday
  • 15. WordPress & Twig
  • 16. Twig: the flexible, fast, and securetemplate engine for PHP.Twig uses a syntax similar to theDjango and Jinja templatelanguages which inspired the Twigruntime environment.
  • 17. Why using a Template Engine ?
  • 18. PHP doesnt support many features moderntemplate languages should have nowadays o Concision o Template oriented Syntax o Reusability o "Security" by default  Ill explain in the next slides  Read more on dedicated webpages.Read more at: http://fabien.potencier.org/article/34/templating-engines-in-php
  • 19. ConcisionPHP TWIG<?php echo $name ?> {{name}}<?php echo {{name|escape}} htmlspecialchars( $name, ENT_QUOTES, No short version its UTF-8) already the short?>Short tags version.<?=$name?>Using short tags could creates problems. You have to enable the short tags that are disabled by default.
  • 20. Template oriented SyntaxPHP TWIG<?php if ($items): ?> {% for item in items %} <?php foreach ($items as $item): ?> * {{ item }} * <?php echo $item ?> {% else %} <?php endforeach; ?> No item has been found.<?php else: ?> {% endfor %} No item has been found. <?php endif; ?> Its more clear and use a new syntax.
  • 21. Reusability<!-- base.html --> <!-- index.html --><html {% extends "base.html" %} xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> {% block head %} <head> {{ block.super }} {% block head %} <link rel="stylesheet" href="main.css" /> <link rel="stylesheet" href="main.css" /> {% endblock %} {% endblock %} </head> {% block content %} <body> Index content {% block content %}{% endblock %} {% endblock %} </body></html>
  • 22. Security" Im not saying PHP is not a secure language, far from it. But needless to say thatescaping a variable in a template is just a nightmare." Sometimes, security should be enabled by default, especially for templates writtenby non-developers who are not necessarily aware of the common web threats like XSSor CSRF. @fabpotPHP TWIG Output is escaped by default.<?php echo htmlspecialchars( {% autoescape off %} $var, {{ object.as_html }} ENT_QUOTES, {% endautoescape %} UTF-8) ?> Im well aware of the automatic output escaping problems.
  • 23. Get TwigWebsite: http://twig.sensiolabs.orgTwig C Extensions: https://github.com/derickr/twig-ext.gitUsing composer:Create a new composer.json file{ "require": { "twig/twig": "1.*" }}curl -s http://getcomposer.org/installer | phpphp composer.phar install
  • 24. Twig & WordPressUsing Twig into a wordpress plugin/theme could simplify and improve the development.• Plugin development (views)• Theme development
  • 25. Integrating Twig into a PluginJust initialize Twig and use it. Using a static reference to share the Twig instance (simple way).function get_twig(){ static $twig; if (!$twig) { $loader = new Twig_Loader_Filesystem(__DIR__ . /templates); $twig = new Twig_Environment($loader, array(cache => false)); } return $twig;}
  • 26. Render a templateDuring the Twig initialization we specify the view location ("templates" folder). Now you can create a Twig view and save it. Into your plugin just link to it:$template = get_twig()->loadTemplate(tweets.twig);echo $template->render(array(elements => $unpack));In this example we load the "tweets.twig" view and pass the elements variable.
  • 27. WP Plugin ExampleYou can checkout an example at:• https://github.com/wdalmut/wp-simple-tweetsIn this example we add a widget into the admin dashboard that shows tweets of @CorleyCloud account.
  • 28. Make a template using TwigWordPress is designed to use PHP as template engine. We can force to integrate Twig as the default templating system. You can find an example of integration at:https://github.com/wdalmut/wp-twig-themeThis example realize a simple blank template with some WP functionalities (sidebars, posts, pages, archives).
  • 29. Making a Twig ProxyWordPress uses a lot of functions to generate pieces of html. To speedup the Twig integration we can create a simple proxy that enable WP functions into a Twig template.class TwigProxy { public function __call($function, $arguments) { if (!function_exists($function)) { throw new Exception("{$function} not exists."); return NULL; } return call_user_func_array($function, $arguments); }}
  • 30. Using the Twig ProxyThis proxy allows us to call WordPress functions into a view.<div id="sidebar"> {% if not wp.dynamic_sidebar("Sidebar Widgets") %} {{ wp.get_search_form }}<!-- continue --></div>"dynamic_sidebar" and "get_search_form" are WP functions.
  • 31. Load Twig templates instead PHPsWe can use WordPress hooking system to override the default template loading.• add_filter o home_template o single_template o page_template o 404_template o archive_template o Etc, etc...• add_action o template_include  Engage Twig and prepare data to view.
  • 32. Layout managementInstead of use get_header and get_footer that break HTML we can use template inheritance of Twig.{% extends "base.twig" %}{% block title %} {{ parent() }} | Page Not Found{% endblock %}{% block content %}<h2>Error 404 - Page Not Found</h2>{% endblock %}
  • 33. Enjoy with TwigTwig is extremely simple and powerful, you can play and enjoy with this great template engine and get more power during your wp development. Have a nice day
  • 34. ?
  • 35. Thank you! Walter Dal Mut walter.dalmut@gmail.com @wdalmut Maurizio Pelizzone maurizio@mavida.com @miziomon