<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Diem Project Snippets</title>
    <link>http://diem-project.org/snippets</link>
    <description></description>
    <pubDate>Tue, 09 Mar 2010 06:41:12 +0100</pubDate>
    <managingEditor>Thibault Duplessis</managingEditor>
    <language>en</language>
    <item>
      <title><![CDATA[Change default submit caption]]></title>
      <link>http://diem-project.org/community/snippets/change-default-submit-caption</link>
      <description><![CDATA[<p class="dm_first_p">In most cases echo $form in template suited fine, but if you want replace only submit button which shows as "Validate [Validate]", you need rewrite whole _form.php template.<br />
This is not a DRY way, because you may change form, then you need change template too.<br />
To avoid this just modify your form, by adding such method:</p>

<pre class="php" style="font-family:monospace;"><span class="kw2">public</span> <span class="kw2">function</span> render<span class="br0">&#40;</span><span class="re0">$attributes</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>  
  <span class="br0">&#123;</span>  
    <span class="re0">$attributes</span> <span class="sy0">=</span> dmString<span class="sy0">::</span><span class="me2">toArray</span><span class="br0">&#40;</span><span class="re0">$attributes</span><span class="sy0">,</span> <span class="kw4">true</span><span class="br0">&#41;</span><span class="sy0">;</span>  
&nbsp;
    <span class="kw1">return</span>  
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">open</span><span class="br0">&#40;</span><span class="re0">$attributes</span><span class="br0">&#41;</span><span class="sy0">.</span>  
    <span class="st_h">'&lt;ul class=&quot;dm_form_elements&quot;&gt;'</span><span class="sy0">.</span>  
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getFormFieldSchema</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$attributes</span><span class="br0">&#41;</span><span class="sy0">.</span>  
      <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st_h">'&lt;li class=&quot;dm_form_element&quot;&gt;&lt;hr size=&quot;1&quot; noshade color=&quot;silver&quot;&gt;%s&lt;/li&gt;'</span><span class="sy0">,</span>  
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">renderSubmitTag</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span>__<span class="br0">&#40;</span><span class="st_h">'send comment'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>  
      <span class="br0">&#41;</span><span class="sy0">.</span>  
    <span class="st_h">'&lt;/ul&gt;'</span><span class="sy0">.</span>  
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>  
  <span class="br0">&#125;</span>  </pre>]]></description>
      <guid isPermaLink="false">Change default submit caption (12)</guid>
      <pubDate>Tue, 09 Mar 2010 06:41:12 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Include JavaScript files in the <head> section]]></title>
      <link>http://diem-project.org/community/snippets/include-javascript-files-in-the-head-section</link>
      <description><![CDATA[<p class="dm_first_p">By default with Diem, all JavaScript files are compressed and combined into one file. This file is loaded just before &lt;/body&gt;, as recommended by <a class="link" href="http://developer.yahoo.com/performance/rules.html#js_bottom">Yahoo Best Practices</a>.</p>

<p>Some symfony plugins write JavaScript code directly into the HTML. They expect jQuery to be loaded before their JavaScript code executes.<br />
So we need a way to include jQuery from &lt;head&gt; section in order to make these plugins work.</p>

<p>Add the assets you want to include in &lt;head&gt; to the js head_inclusion configuration:</p>

<p><em>config/dm/config.yml</em></p>

<pre><code>all:  
  
  js:  
    compress:             true  
    cdn:  
      enabled:            false  
    head_inclusion:       [ lib.jquery ]  </code></pre>]]></description>
      <guid isPermaLink="false">Include JavaScript files in the <head> section (11)</guid>
      <pubDate>Thu, 04 Feb 2010 13:42:17 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Improve performances: gzip assets]]></title>
      <link>http://diem-project.org/community/snippets/improve-performances-gzip-assets</link>
      <description><![CDATA[<p class="dm_first_p"><em>This snippet will only work with <strong>apache2</strong> servers.</em></p>

<h2 id="problem">Problem</h2>

<p>Bandwidth has a cost, and performances matters.</p>

<p>By default, Diem <strong>minifies and combines</strong> all your javascript and stylesheet files. This reduces dramatically the number of requests required to display a page.</p>

<p>But we can go even farther.<br />
You can enable <strong>gzip compression</strong> for javacript and stylesheet files. It reduces their weight by three to four times.</p>

<h2 id="enable-gzip-compression">Enable gzip compression</h2>

<p>Just change an option on Diem javascript and stylesheet compressors to make them produce gziped files.<br />
<em>config/dm/services.yml</em></p>

<pre><code>parameters:  
  
  javascript_compressor.options:    
    gz_compression: true  
  
  stylesheet_compressor.options:    
    gz_compression: true  </code></pre>

<h2 id="forward-assets-requests">Forward assets requests</h2>

<p>Now, we will tell apache to forward requests to a .css or .js to the gzipped file, if any.<br />
<em>web/.htaccess</em>, uncomment the lines between<br />
  &#35; SEND GZIPPED CONTENT TO COMPATIBLE BROWSERS<br />
and<br />
  # END GZIPPED CONTENT</p>

<p>You should now have:</p>

<pre><code>  # SEND GZIPPED CONTENT TO COMPATIBLE BROWSERS  
  RemoveType .gz  
  RemoveOutputFilter .css .js  
  AddEncoding x-gzip .gz  
  AddType "text/css;charset=utf-8" .css  
  AddType "text/javascript;charset=utf-8" .js  
  RewriteCond %{HTTP:Accept-Encoding} gzip  
  RewriteCond %{REQUEST_FILENAME}.gz -f  
  RewriteRule ^(.*)$ $1.gz [L,QSA]  
  # END GZIPPED CONTENT  </code></pre>

<h2 id="try-it">Try it</h2>

<p>Open a page of your website and inspect requests with firebug. The server now sends gzipped assets, and load time is reduced.</p>

<p>This tip will not work on all servers, depending on apache configuration.</p>]]></description>
      <guid isPermaLink="false">Improve performances: gzip assets (10)</guid>
      <pubDate>Tue, 19 Jan 2010 22:10:32 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Customize the breadcrumb]]></title>
      <link>http://diem-project.org/community/snippets/customize-the-breadcrumb</link>
      <description><![CDATA[<p class="dm_first_p">On the front application, breadcrumbs are generated automatically.</p>

<p>Sometimes we want to modify the displayed links.</p>

<h2 id="the-easy-way-listen-an-event">The easy way: listen an event</h2>

<p>Use the <a class="link" href="/diem-5-0/doc/en/reference-book/events#events-list:front-events:dm-bread_crumb-filter_links" title="Reference Book - Events | Documentation">dm.bread_crumb.filter_links event</a> to modify the breadcrumb links before the are rendered.<br />
The event contains a <strong>page</strong> parameter which is the current page.<br />
The event value is an array of the breadcrumb links to be displayed. We will modify the links array to modify the breadcrumb.</p>

<p>In this example, we assume we have a <strong>post</strong> module with <strong>post/list</strong> and <strong>post/show</strong> pages.<br />
We will rename the <strong>post/list</strong> link, or remove it when the current page is a <strong>post/show</strong> page.</p>

<p><em>apps/front/config/frontConfiguration.class.php</em></p>

<pre class="php" style="font-family:monospace;"><span class="kw2">class</span> frontConfiguration <span class="kw2">extends</span> dmFrontApplicationConfiguration
<span class="br0">&#123;</span>  
&nbsp;
  <span class="kw2">public</span> <span class="kw2">function</span> configure<span class="br0">&#40;</span><span class="br0">&#41;</span>  
  <span class="br0">&#123;</span>  
    <span class="co1">// connect to the breadcrumb event  </span>
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getEventDispatcher</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">connect</span><span class="br0">&#40;</span><span class="st_h">'dm.bread_crumb.filter_links'</span><span class="sy0">,</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">,</span> <span class="st_h">'listenToBreadCrumbFilterLinksEvent'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>  
  <span class="br0">&#125;</span>  
&nbsp;
  <span class="kw2">public</span> <span class="kw2">function</span> listenToBreadCrumbFilterLinksEvent<span class="br0">&#40;</span>sfEvent <span class="re0">$e</span><span class="sy0">,</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a> <span class="re0">$links</span><span class="br0">&#41;</span>  
  <span class="br0">&#123;</span>  
    <span class="co1">// check if the breadcrumb contains a link to the post/list page  </span>
    <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$links</span><span class="br0">&#91;</span><span class="st_h">'post/list'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>  
    <span class="br0">&#123;</span>  
      <span class="co1">// if the current page is a post/show page  </span>
      <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$e</span><span class="br0">&#91;</span><span class="st_h">'page'</span><span class="br0">&#93;</span><span class="sy0">-&gt;</span><span class="me1">isModuleAction</span><span class="br0">&#40;</span><span class="st_h">'post'</span><span class="sy0">,</span> <span class="st_h">'show'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>  
      <span class="br0">&#123;</span>  
        <span class="co1">// remove the post/list link  </span>
        <a href="http://www.php.net/unset"><span class="kw3">unset</span></a><span class="br0">&#40;</span><span class="re0">$links</span><span class="br0">&#91;</span><span class="st_h">'post/list'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>  
      <span class="br0">&#125;</span>  
      <span class="kw1">else</span>  
      <span class="br0">&#123;</span>  
        <span class="co1">// Rename the link  </span>
        <span class="re0">$links</span><span class="br0">&#91;</span><span class="st_h">'post/list'</span><span class="br0">&#93;</span><span class="sy0">-&gt;</span><span class="me1">text</span><span class="br0">&#40;</span><span class="st_h">'Articles'</span><span class="br0">&#41;</span><span class="sy0">;</span>  
      <span class="br0">&#125;</span>  
    <span class="br0">&#125;</span>  
&nbsp;
    <span class="co1">// return the links to the breadcrumb  </span>
    <span class="kw1">return</span> <span class="re0">$links</span><span class="sy0">;</span>  
  <span class="br0">&#125;</span>  </pre>

<h2 id="the-powerful-way-override-the-class">The powerful way: override the class</h2>

<p>You can also override the breadcrumb view class. First, declare the new view class for breadcrumb widgets:</p>

<p><em>apps/front/config/dm/widget_types.yml</em></p>

<pre><code>dmWidgetNavigation:  
  
  breadCrumb:  
    view_class:         myBreadCrumbView  </code></pre>

<p>Then create your class, which inherits dmWidgetNavigationBreadCrumbView.<br />
<em>apps/front/lib/myBreadCrumbView</em></p>

<pre class="php" style="font-family:monospace;"><span class="kw2">class</span> myBreadCrumbView <span class="kw2">extends</span> dmWidgetNavigationBreadCrumbView
<span class="br0">&#123;</span>  
  <span class="co1">// override the getLinks method  </span>
  protected <span class="kw2">function</span> getLinks<span class="br0">&#40;</span><span class="re0">$includeCurrent</span> <span class="sy0">=</span> <span class="kw4">true</span><span class="br0">&#41;</span>  
  <span class="br0">&#123;</span>  
    <span class="co1">// get links from dmWidgetNavigationBreadCrumbView  </span>
    <span class="re0">$links</span> <span class="sy0">=</span> parent<span class="sy0">::</span><span class="me2">getLinks</span><span class="br0">&#40;</span><span class="re0">$includeCurrent</span><span class="br0">&#41;</span><span class="sy0">;</span>  
&nbsp;
    <span class="co1">// always remove the home link (for example)  </span>
    <a href="http://www.php.net/unset"><span class="kw3">unset</span></a><span class="br0">&#40;</span><span class="re0">$links</span><span class="br0">&#91;</span><span class="st_h">'main/root'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>  
&nbsp;
    <span class="kw1">return</span> <span class="re0">$links</span><span class="sy0">;</span>  
  <span class="br0">&#125;</span>  
<span class="br0">&#125;</span>  </pre>

<p>This way you can override all the breadcrumb view methods.</p>

<p>With the <em>apps/front/config/dm/widget_types.yml</em> config file, you can replace every form or view class for all widgets, and even create yours.</p>]]></description>
      <guid isPermaLink="false">Customize the breadcrumb (9)</guid>
      <pubDate>Thu, 14 Jan 2010 15:45:48 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Exclude files from JS minification]]></title>
      <link>http://diem-project.org/community/snippets/exclude-files-from-js-minification</link>
      <description><![CDATA[<p class="dm_first_p">Diem minify javascript files automatically, and merge them into one file, which is compressed with gzip.<br />
This is a <strong>good thing</strong>.</p>

<p>But some javascript libraries don't support compression, like <strong>jquery.lightbox-0.5.js</strong>.</p>

<p>In this case you may disable all javascript minification in <em>config/dm/config.yml</em></p>

<pre><code>all:  
  js:  
    compress: false  </code></pre>

<p>But it's a <strong>bad solution</strong>: none of your javascript files is minified anymore.</p>

<p>The <strong>better solution</strong> is to disable minification only for <strong>jquery.lightbox-0.5.js</strong>.</p>

<p>This can be done by configuring the <strong>javascript_compressor</strong> service.</p>

<p><em>config/dm/services.yml</em></p>

<pre><code>parameters:  
  javascript_compressor.options:  
    black_list:               [ jquery.lightbox-0.5.js ]  </code></pre>

<p>Then clear the cache, and all the javascript files are minified, excepted this one.</p>]]></description>
      <guid isPermaLink="false">Exclude files from JS minification (8)</guid>
      <pubDate>Fri, 08 Jan 2010 08:34:34 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Diem and Nginx]]></title>
      <link>http://diem-project.org/community/snippets/diem-and-nginx</link>
      <description><![CDATA[<p class="dm_first_p">Diem can't be easily used in combination with NGINX. Nginx replaces the + with  %2B in the url. Which is vital for the Diem routing.</p>

<p>But there is a very easy fix:<br />
in config/ProjectConfiguration.class.php put</p>

<pre><code>$_SERVER['PATH_INFO'] =urldecode($_SERVER['PATH_INFO']);   </code></pre>

<p>in the setup() method.</p>]]></description>
      <guid isPermaLink="false">Diem and Nginx (7)</guid>
      <pubDate>Tue, 29 Dec 2009 12:01:31 +0100</pubDate>
    </item>
    <item>
      <title><![CDATA[Create a manageable menu]]></title>
      <link>http://diem-project.org/community/snippets/create-a-manageable-menu</link>
      <description><![CDATA[<p class="dm_first_p">In this snippet we will create a manageable menu.</p>

<hr />

<p><strong>Deprecated snippet</strong><br />
Diem now provides a much better way to create and manage menus without any technical knowledge. Learn more about the <a class="link" href="/diem-5-0/doc/en/reference-book/widgets#built-in-widgets:navigation:menu" title="Reference Book - Widgets | Documentation">new menu widget</a>.</p>

<hr />

<p>The links must be manageable from the admin application.<br />
They must be activable/deactivable, and sortable. They can be internal or external links.</p>

<h2 id="declare-the-link-model-and-module">Declare the link model and module</h2>

<p>schema.yml</p>

<pre><code>Link:  
  actAs:              [ Timestampable, Sortable ]  
  columns:  
    name:             { type: string(255), notnull: true }  
    title:            { type: string(255), notnull: false }  
    url:              { type: string(255), notnull: true, extra: link }  
    is_active:        { type: boolean, notnull: true, default: true }  
</code></pre>

<p>modules.yml</p>

<pre><code>Content:  

  Global:  

    link:  
      components:  
        list:  
</code></pre>

<h2 id="update-the-project">Update the project</h2>

<pre><code>php symfony doctrine:generate-migrations-diff  

php symfony doctrine:migrate  

php symfony dm:setup  
</code></pre>

<h2 id="create-links-in-admin">Create links in admin</h2>

<p>In the Content menu of the admin upper toolbar, a links section is now available.</p>

<h3 id="create-links-in-admin:internal-links">Internal links</h3>

<p>We can drag &amp; drop a page from the PAGES left panel into the "url" form field.<br />
This way, if the page url change later, the link will be automatically updated.</p>

<h3 id="create-links-in-admin:external-links">External links</h3>

<p>We can write full urls like http://symfony-project.org</p>

<p>We can also use the "Loremize" box to automate test data creation.</p>

<h2 id="create-the-front-interface">Create the front interface</h2>

<p>Click the "Add" button on the lower tool bar, and drag &amp; drop a Link/List widget on the page.<br />
Then edit the template /apps/front/modules/link/templates/_list.php and replace "echo $link" with:</p>

<pre class="php" style="font-family:monospace;"><span class="kw1">echo</span> _link<span class="br0">&#40;</span><span class="re0">$link</span><span class="sy0">-&gt;</span><span class="me1">url</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">title</span><span class="br0">&#40;</span><span class="re0">$link</span><span class="sy0">-&gt;</span><span class="me1">title</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">text</span><span class="br0">&#40;</span><span class="re0">$link</span><span class="sy0">-&gt;</span><span class="me1">name</span><span class="br0">&#41;</span><span class="sy0">;</span></pre>

<p>Now we have a menu widget as specified above.</p>]]></description>
      <guid isPermaLink="false">Create a manageable menu (6)</guid>
      <pubDate>Wed, 16 Dec 2009 15:29:27 +0100</pubDate>
    </item>
  </channel>
</rss>