Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
14 posts Page 1 of 2
Hi, some cool new tag here: <cms:trim />.
EDIT: Also read new posts below for adding new functionality for stripping off HTML comments and PHP-like comments. More details also given below about enabling automatic minify for all code generated by CouchCMS.

New tag <cms:trim /> can normalize white-spaces, removing double spaces and trimming space-like characters (new lines, tabs, etc) from both ends of the string passed to it.

I got inspiration to make up this tag after reading an article here http://pageconfig.com/post/remove-undes ... im_all-php

CouchCMS 2.0+ has some part of this enabled by default with removing only spaces caused by tags (not exactly the same implementation). So <cms:trim /> can extend this to a greater level.

For all Couch versions <cms:trim /> is cool for minifying purposes.

Usage: <cms:trim var />
(<cms:show var /> can be safely changed to <cms:trim var />)
For snippets:
Code: Select all
<cms:trim "<cms:embed 'snippet.html' />" />

Works great and fast.

Hope this helps minify some byte-waste across the planet. :D

Important tip to use with large sparse chunks, like those, generated by html/css layout, cms:pages etc.: cms:trim works like <cms:show />, so it is possible to do this thing:
Code: Select all
<cms:capture into='page_listing' >
          ... whatever ...
</cms:capture>
<cms:trim page_listing />

The code above saves whatever into some new variable named page_listing and outputs it minified.

P.S. Welcome with suggestions and mod requests.
Code: Select all

    function trim_all( $str, $node ){

        $res = $str[0]['rhs'];
        return trim( preg_replace( "/\s+/" , ' ' , $res ) , " \t\n\r\0\x0B" );
    }



Code above is a simplified version of 'trim', which removes multiple spaces and new lines. Previous version was seen to remove double zeros in the end of the string, like '500' becomes '5', if nothing is after it. This posted version will not do it, I gladly report after hitting this wall myself. :)
trendoman can you illustrate the effect of this trick - for example picture screen
thanks
@orbital, if you check generated source in browser via source-view or Inspect in chrome, you can see that in couch 1.4.7 generated html-code has many empty lines. Sometimes empty space is a result of embedding snippets with calculations.

cms:trim helps remove those spaces.
Yes - I know that, but I already have version 2 - then this code does it matter?
Hi Mate!

Do you have a live site that i can take a look how the minifying output on the page source?


Thx!
As soon as possible!

Touch me up : abada[dot]zulma[at]gmail[dot]com
Code updated. Now removes extra spaces without errors.
Now sample.
Before:
ScreenCut-12-06---13-41-14-.png
Without <cms:trim />
ScreenCut-12-06---13-41-14-.png (13.35 KiB) Viewed 9525 times

After:
ScreenCut-12-06---13-41-35-.png
With <cms:trim />
ScreenCut-12-06---13-41-35-.png (39.33 KiB) Viewed 9525 times


Handy for massive number of opt_values in selects/checkboxes/dropdowns, also handy for embedding snippets with sparse code.
Now usage.
Code: Select all
Instead of <cms:show some_variable /> use <cms:trim some_variable /> - it will show trimmed output.

For snippets can be used like this:
Code: Select all
<cms:trim "<cms:embed 'snippet.html' />" />
Code improved and updated.
Sometimes total minification is required :)

Then the result would look like this:
ScreenCut-12-26---12-04-43-.png
Minification Sample
ScreenCut-12-26---12-04-43-.png (195.96 KiB) Viewed 9466 times
Update. Now leaving alone "<![CDATA[ " (essential for CKEditor to work) and other improvements.

After more thorough study I realized that writing a good minify plugin from scratch would be a mistake.
14 posts Page 1 of 2