0

I have some problem with the Assets class to minify and combine my css and js. I have this plugin ad it seems to work without problem:

class Plugin_Theme_Assets extends Plugin
{
    /**
    * combine and insert multiple js files
    *
    * usage:
    * { theme_assets:js_files files="file1.js,file2.js" }
    */
    function js_files()
    {
        $files = $this->attribute('files');
        preg_match_all('/[\w-\.]+\.js/i', $files, $files_a);
        foreach($files_a[0] as $file)
        {
            Asset::js($file);
        }
        return Asset::render_js();
    }

    /**
    * combine and insert multiple css files
    *
    * usage:
    * { theme_assets:css_files files="file1.css,file2.css" }
    */
    function css_files()
    {
        $files = $this->attribute('files');
        preg_match_all('/[\w-\.]+\.css/i', $files, $files_a);
        foreach($files_a[0] as $file)
        {
            Asset::css($file);
        }
        return Asset::render_css();
    }
}

This is my .htaccess file and as you can si i have uncomment the SetEnv:

# Multiple Environment config
# Set this to development, staging or production
  SetEnv PYRO_ENV production

<IfModule mod_rewrite.c>

    # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    RewriteBase /

    # Restrict your site to only one domain
    # !important USE ONLY ONE OPTION

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With}   !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST}             ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$           $1 [R=301,NS,L]

    # Keep people out of codeigniter directory and Git/Mercurial data
    RedirectMatch 403 ^/(system\/cms\/cache|system\/codeigniter|\.git|\.hg).*$

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    <IfModule mod_php5.c>
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

</IfModule>

so, if i'm right the PYRO_ENV now is "production".

If i refresh my page the css and the js file are not minify and combine into a single file... Here what the plugin return

<link href="http://example:8888/addons/default/themes/mytheme/css/nivo-slider.css" rel="stylesheet" type="text/css" />
<link href="http://example:8888/addons/default/themes/mytheme/css/default.css" rel="stylesheet" type="text/css" />

Can you please help me?

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113

3 Answers3

3

If your hosting will not allow SetEnv in .htaccess, you can use the Rewrite rules to send an environment variable:

RewriteCond %{HTTP_HOST} ^local.yoursite.com$
RewriteRule (.*) $1 [E=PYRO_ENV:development]

RewriteCond %{HTTP_HOST} ^beta.yoursite.com$
RewriteRule (.*) $1 [E=PYRO_ENV:staging]

RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule (.*) $1 [E=PYRO_ENV:production]

The Asset minification and combine will occur by default on a production or staging system, development environments won't minify and combine for development/debugging purposes. You can override this in system/cms/config/asset.php for testing purposes.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
1

This is a plugin you've written yourself, right? I'm confused why in the docs you're suggesting people use { theme_assets:css_files files="file1.css,file2.css" } when you can just do this without needing a custom plugin at all:

{{ asset:css file="file1.css" }}
{{ asset:css file="file2.css" }}
{{ asset:render }}

(Also yes, it should just work once you've set the environment correctly (you can also modify /system/cms/config/asset.php for testing).

William Turrell
  • 3,227
  • 7
  • 39
  • 57
0

The problem was my Hosting, it doesn't allow me to use SetEnv in .htaccess

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113